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

Model::hasChanged() and getChangedFields() return false values #14376

Closed
zsilbi opened this issue Sep 10, 2019 · 2 comments
Closed

Model::hasChanged() and getChangedFields() return false values #14376

zsilbi opened this issue Sep 10, 2019 · 2 comments
Labels
bug A bug report status: low Low

Comments

@zsilbi
Copy link
Member

zsilbi commented Sep 10, 2019

When snapshot data is saved to a Model, all attributes are being saved as string.
There can be issues regardless orm.cast_on_hydrate is turned on or off.

Here is a quick example:

class Robots extends \Phalcon\Mvc\Model
{
    public $id; // unsigned int(10) in MySQL
    public $year; // unsigned int(10) in MySQL

    public function initialize()
    {
        $this->keepSnapshots(true);
    }
}

/****************************************/
ini_set('phalcon.orm.cast_on_hydrate', 0);

$robot = Robots::findFirst();

var_dump($robot->toArray()); // array(2) { ["id"] => string(1) "1" ["year"] => string(4) "1900" }

var_dump($robot->year); // string(4) "1900"

var_dump($robot->getSnapshotData()['year']); // string(4) "1900"

var_dump($robot->hasChanged('year')); // bool(false)

$robot->year = 1900;
var_dump($robot->hasChanged('year')); // bool(true)

$robot->year = "1900";
var_dump($robot->hasChanged('year')); // bool(false)

/****************************************/
ini_set('phalcon.orm.cast_on_hydrate', 1);

$robot = Robots::findFirst();

var_dump($robot->toArray()); // array(2) { ["id"] => int(1) ["year"] => int(1900) }

var_dump($robot->year); // int(1900)

var_dump($robot->getSnapshotData()['year']); // string(4) "1900"

var_dump($robot->hasChanged('year')); // bool(true)

$robot->year = 1900;
var_dump($robot->hasChanged('year')); // bool(true)

$robot->year = "1900";
var_dump($robot->hasChanged('year')); // bool(false)

When orm.cast_on_hydrate is on, I think Model::cloneResultMap() should save the casted values to the snapshot data.
If it's off, then Model::getChangedFields() should not use === to check if the field has changed.

Any thoughts?

Details

  • Phalcon version: 4.0.x branch
@ruudboon
Copy link
Member

cloneResultMap should save casted values when cast_on_hydrate is on.

I'm not sure if I agree not to use === when cast_on_hydrate is off.
When cast_on_hydrate is off you should accept that all your model properties are strings and should set them as string. If you set it as int I'm expecting that hasChanged will detect a change.

@zsilbi
Copy link
Member Author

zsilbi commented Sep 18, 2019

Resolved in #14377

@zsilbi zsilbi closed this as completed Sep 18, 2019
@niden niden added bug A bug report status: low Low and removed Bug - Low labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: low Low
Projects
None yet
Development

No branches or pull requests

3 participants