Tigrov/yii2-pgsql

save composite type from model

kr0lik opened this issue · 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();

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