Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add a before hook to an association table? #324

Closed
ghost opened this issue Sep 3, 2013 · 6 comments
Closed

Is it possible to add a before hook to an association table? #324

ghost opened this issue Sep 3, 2013 · 6 comments

Comments

@ghost
Copy link

ghost commented Sep 3, 2013

I have defined an hasMany relationship and it works as I would assume it does but for some reason the beforeCreate hook on the association table is not called.

@ghost
Copy link
Author

ghost commented Sep 4, 2013

I tried to explore the source and was having difficulty getting into the mind set of the code. It seems to me @ first glance thought that it is not possible to define hooks an association tables.

Is the association table though a "Model" object though? If not would it make sense to make it a Model object? That way you can have more control over the handling of data stored on the edge.

@dresende
Copy link
Owner

What exactly do you want to do? What do you mean by association table, the 3rd table or the table of the other model?

@ghost
Copy link
Author

ghost commented Sep 16, 2013

I want to add validations to the 3rd tables data ( i believe the term is
join table, the one that sits between the two main entities )

I took some time to go through the source and it didnt seem that the 3rd
table was a logical extension of an "entity" but a hard coded between two
entities. ( I don`t know if this comment make sense to you )

@dresende
Copy link
Owner

Yes, I understand. I'll see if I can force a new before* hook.

dresende added a commit that referenced this issue Sep 16, 2013
@dresende
Copy link
Owner

Usage:

Person.hasMany("pets", Pet, {
    born : Date
}, {
    hooks : {
        beforeSave : function (props, next) {
            // props is { born: ... } or empty {}
            // `this` is a reference to the pet being associated
            return next();
        }
    }
});

If no extra properties defined, hook will not pass props at all.

Person.hasMany("pets", Pet, {}, {
    hooks : {
        beforeSave : function (next) {
            return next(new Error('no associations allowed')); // error example
        }
    }
});

@dresende
Copy link
Owner

Please test by installing from git:

npm install dresende/node-orm2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant