yii2-pgsql icon indicating copy to clipboard operation
yii2-pgsql copied to clipboard

save composite type from model

Open kr0lik opened this issue 8 years ago • 1 comments

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();

kr0lik avatar Sep 13 '17 20:09 kr0lik

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([...]);

Tigrov avatar Sep 14 '17 14:09 Tigrov