The InsertUpdateCommandBehavior extension adds up some functionality to the default possibilites of yii´s CDbCommand implementation. Creates and executes an INSERT
with ON DUPLICATE KEY UPDATE
MySQL statement.
- Yii Framework 1.1.14 or above
- Composer
$ composer require dotzero/yii-insertupdate-behavior
'aliases' => array(
...
'vendor' => realpath(__DIR__ . '/../../vendor'),
),
'import' => array(
...
'vendor.dotzero.yii-insertupdate-behavior.*',
),
$command = Yii::app()->db->createCommand();
$command->attachBehavior('InsertUpdateCommandBehavior', new InsertUpdateCommandBehavior);
$command->insertUpdate('tbl_user', array(
'name'=>'Tester',
'email'=>'[email protected]',
'counter'=>'1'
), array(
'name'=>'Tester',
'email'=>'[email protected]'
));
Creates and executes an INSERT
with ON DUPLICATE KEY UPDATE
MySQL statement
INSERT INTO `tbl_user` (`name`, `email`, `counter`)
VALUES ('Tester', '[email protected]', 1)
ON DUPLICATE KEY UPDATE `name`='Tester', `email`='[email protected]';
$command = Yii::app()->db->createCommand();
$command->attachBehavior('InsertUpdateCommandBehavior', new InsertUpdateCommandBehavior);
$command->insertUpdate('tbl_user', array(
'name'=>'Tester',
'email'=>'[email protected]',
'counter'=>'1'
), array(
'name'=>'Tester',
'email'=>'[email protected]'
'counter'=>new CDbExpression('LAST_INSERT_ID(counter)');
));
Creates and executes an INSERT
with ON DUPLICATE KEY UPDATE
MySQL statement
INSERT INTO `tbl_user` (`name`, `email`, `counter`)
VALUES ('Tester', '[email protected]', 1)
ON DUPLICATE KEY UPDATE `name`='Tester', `email`='[email protected]', `counter`=LAST_INSERT_ID(counter);
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php