Skip to content

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

Closed

Description

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.

Activity

  1. dresende commented on Sep 13, 2013

    @dresende
    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?

  2. dresende commented on Sep 16, 2013

    @dresende
    Owner

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

  3. dresende commented on Sep 16, 2013

    @dresende
    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
            }
        }
    });
  4. dresende commented on Sep 16, 2013

    @dresende
    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions