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

hasOne delAccessor not working #226

Closed
ctaepper opened this issue Jul 14, 2013 · 8 comments
Closed

hasOne delAccessor not working #226

ctaepper opened this issue Jul 14, 2013 · 8 comments
Assignees

Comments

@ctaepper
Copy link

i define a status for a person:

Person.hasOne('status', Status, {
    autoFetch: true
});

i can set and change that status, but i cannot remove it:

person.removeStatus(callback);

i add beforeSave and afterSave hooks to the Person Model:

hooks: {
    beforeSave: function(next) {
        console.log('beforeStatus', this.status_id);
        next();
    },
    afterSave: function() {
        console.log('afterStatus', this.status_id);
    }
}

it seems like the hooks are called 3 times (???)
beforeSave the first time tells me the expected value (this.status_id = null)
but before afterSave is called, beforeSave is called another 2 times having the original value of status_id... after those 3 beforeSave calls, the 3 afterSave calls are called, each of them having the original value of status_id in it, and not my expected value null

help?

@dresende
Copy link
Owner

I'm going to take a closer look.

@ghost ghost assigned dresende Jul 16, 2013
@dresende
Copy link
Owner

I actually see 2 beforeSave calls and 2 afterSave calls. Are you sure it's 3 beforeSave ? Can you show me your Person model ?

@dresende
Copy link
Owner

In my test file, there are 2 calls because somehow the Model is making an UPDATE query to set status_id to NULL and then making a new UPDATE query to set status_id to the previous one..

@ctaepper
Copy link
Author

var _s = require('underscore.string');
var _ = require('underscore');
var ImageResource = require('./imageresource');
var Status = require('./status');
var async = require('async');

var Person = null;

module.exports = {

    init: function (db, cb) {
        Person = db.define("person", {
            title: String,
            firstname: String,
            lastname: String,
            job: String,
            phone: String,
            email: String,
            sort: Number,

            created_at: {type: "date", time: true},
            edited_at: {type: "date", time: true}
        });

        Person.hasOne('imageresource', ImageResource.model(), {
            autoFetch: true
        }).hasOne('status', Status.model(), {
            autoFetch: true
        });
        cb();
    },


    create: function (data, cb) {
        Person.create({
            title: data.title,
            firstname: data.firstname,
            lastname: data.lastname,
            job: data.job,
            phone: data.phone,
            email: data.email,
            sort: 999,
            status_id: 1,
            created_at: new Date()
        }, cb)
    },

    setImageResources: function (id, resource, cb) {
        Person.get(id, function (err, person) {
            if (err) return cb(err);

            if (resource && resource.length) {
                person.setImageresource(_.first(resource), cb);
            } else {
                // TODO check github issues for error fix
                person.removeImageresource(cb);
            }
        });
    },

    setStatus: function (id, status, cb) {
        Person.get(id, function (err, person) {
            if (err) return cb(err);
            person.setStatus(status, cb);
        })
    },

    addImageResources: this.setImageResources,

    get: function (id, cb) {
        Person.get(id, cb);
    },


    find: function (options, cb) {
        Person.find(options, cb);
    },


    update: function (id, data, cb) {
        Person.get(id, function (err, obj) {
            if (err) return cb(err);

            delete data['id'];
            delete data['created_at'];

            data.edited_at = new Date();


            obj.save(data, cb);
        });
    },

    remove: function (id, cb) {
        Person.get(id, function (err, obj) {
            if (err) return cb(err);
            obj.remove(cb);
        });
    },

    model: function () {
        return Person;
    }
};


@ctaepper
Copy link
Author

yes, i am 100% sure its 3 calls... in this file it is actually the imageresource i want to remove... but it also works with status

@dresende
Copy link
Owner

I think the hooks are being called 3 times because of the 2 associations. I only have one so it's called 2 times. I'm going to test with more associations.

@dresende
Copy link
Owner

This was nasty to debug, simple to fix :)

In the meantime, if you want to use the git version, do:

npm install dresende/node-orm2

@ctaepper
Copy link
Author

i debugged myself for 2 hours before i gave up and left it to you ;-)
but thanks, will check it out tonight!

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

2 participants