Yii Framework Forum

$model->save and ON DUPLICATE KEY UPDATE clause


(micpan) #1

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’
:))


(Fsb) #2

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?


(micpan) #3

Thanx! All in all I stopped on upsert tool.

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


(Fsb) #4

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