$model->save and ON DUPLICATE KEY UPDATE clause

Hi all!
I load model and try to save it.
How to add ON DUPLICATE KEY clause for set some instruction if record already exists?

All i want is ‘ON DUPLICATE KEY UPDATE some_col = some_col + some_val’
:))

There is yii\db\QueryBuilder::upsert() but I’m not aware of anything like it in Active Record.

What are you trying to accomplish that leads you to ask about ON DUPLICATE KEY UPDATE?

Thanx! All in all I stopped on upsert tool.

it is just addToCart functional: if thing already in cart, just update qty

If you prefer to use AR, you could implement the logic in PHP instead of the DB using yii\db\BaseActiveRecord::$isNewRecord.

1 Like