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

save composite type from model #5

Open
kr0lik opened this issue Sep 13, 2017 · 1 comment
Open

save composite type from model #5

kr0lik opened this issue Sep 13, 2017 · 1 comment

Comments

@kr0lik
Copy link

kr0lik commented Sep 13, 2017

Not work save to db from model, that was created from this type on select from db.

$newModel = Product::findOne($model->id);
$newModel->price; // is new Money(['value' => 10, 'currency_code' => 'USD'])
$newModel->price->value = 20;
$model->save(); - not work. In db still value = 10.

But your exemple with new model works fine:

$newModel = Product::findOne($model->id);
$model->price = new Money([
'value' => 10,
'currency_code' => 'USD'
]);
$model->save();

@Tigrov
Copy link
Owner

Tigrov commented Sep 14, 2017

It related with the method BaseActiveRecord::isAttributeChanged()
https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L579

If you want to change a property of the object $newModel->price you should mark the attribute price as changed. One of the following ways:

  1. $newModel->markAttributeDirty('price');
  2. $newModel->price = clone $newModel->price;
  3. $newModel->price = new Money([...]);

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

No branches or pull requests

2 participants