cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon\Mvc\Model - getChangedFields() after getting related model.

Open naNuke opened this issue 12 years ago • 6 comments

Phalcon: 1.3.1 MySQL: 5.5.35

So I have an issue here with checking changed fields.

This works:

// Find post model.
$post = Post::findFirst(1);
// Set new title.
$post->title = 'new title';
// Did something change?
var_dump($post->getChangedFields());

This does not work: (poster in this case in an alias of belongsTo relationship with user model)

// Find post model.
$post = Post::findFirst(1);
// Get author's model.
$poster = $post->poster; // This causes the exception.
// Set new title.
$post->title = 'new title';
// Did something change?
var_dump($post->getChangedFields());

In the example above the model throws "Change checking cannot be performed because the object has not been persisted or is deleted"

Changing the example to use magic method instead of magic property makes it work again without a problem:

// Find post model.
$post = Post::findFirst(1);
// Get author's model.
$poster = $post->getPoster(); // Magic method call doesn't cause exception anymore.
// Set new title.
$post->title = 'new title';
// Did something change?
var_dump($post->getChangedFields());

Not sure if this is intended behaviour or a bug but it is certainly weird :)

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

naNuke avatar Mar 28 '14 19:03 naNuke

Use $post->getPoster is call the Phalcon\Mvc\Model::__call. Use $post->poster will call the Phalcon\Mvc\Model::__set.

        if (Z_TYPE_P(value) == IS_OBJECT) {
                if (instanceof_function_ex(Z_OBJCE_P(value), phalcon_mvc_modelinterface_ce, 1 TSRMLS_CC)) {
                        PHALCON_INIT_VAR(lower_property);
                        phalcon_fast_strtolower(lower_property, property);
                        phalcon_update_property_zval_zval(this_ptr, lower_property, value TSRMLS_CC);
                        phalcon_update_property_array(this_ptr, SL("_related"), lower_property, value TSRMLS_CC);
                        phalcon_update_property_long(this_ptr, SL("_dirtyState"), 1 TSRMLS_CC);
                        RETURN_CTOR(value);
                }
        }

dreamsxin avatar Mar 29 '14 00:03 dreamsxin

Isn't $post->poster in this case a __get() call instead of __set() ? Should be __set() only when it receives a value like this: $post->poster = UserModel

naNuke avatar Mar 29 '14 02:03 naNuke

In Model::__get when get the poster data will call Model::__set.

dreamsxin avatar Mar 29 '14 03:03 dreamsxin

$post->poster is a __get(), since there's no parenthesis at the end of poster.

I think the issue comes from the fact that $post->poster is not populated at the creation of the model $post. Then, it can't compare the value when $post->getChangedFields() is fired, with an initial empty value.

You should then compare the private value, perhaps $post->posterId in your case. Bug I agree that's a bug: that should be compared to the private value, regardless of the related record (that's bind with it's primary key or something similar).

dugwood avatar Nov 07 '14 08:11 dugwood

Fixed in 2.0.x

andresgutierrez avatar Sep 08 '15 16:09 andresgutierrez

This is broken again somehow, see #11042

nsossonko avatar Oct 21 '15 15:10 nsossonko