dotzero / yii-insertupdate-behavior
The Yii InsertUpdateCommandBehavior extension adds up some functionality to the default possibilites of yii´s CDbCommand implementation
Installs: 37
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/dotzero/yii-insertupdate-behavior
Requires
- php: >=5.3.0
- yiisoft/yii: >=1.1.14
This package is auto-updated.
Last update: 2022-06-04 08:33:56 UTC
README
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.
Requirements:
- Yii Framework 1.1.14 or above
- Composer
Install
Via composer:
$ composer require dotzero/yii-insertupdate-behavior
Add vendor path and import path to your configuration file:
'aliases' => array( ... 'vendor' => realpath(__DIR__ . '/../../vendor'), ), 'import' => array( ... 'vendor.dotzero.yii-insertupdate-behavior.*', ),
Basic usage:
$command = Yii::app()->db->createCommand(); $command->attachBehavior('InsertUpdateCommandBehavior', new InsertUpdateCommandBehavior); $command->insertUpdate('tbl_user', array( 'name'=>'Tester', 'email'=>'tester@example.com', 'counter'=>'1' ), array( 'name'=>'Tester', 'email'=>'tester@example.com' ));
Creates and executes an INSERT
with ON DUPLICATE KEY UPDATE
MySQL statement
INSERT INTO `tbl_user` (`name`, `email`, `counter`) VALUES ('Tester', 'tester@example.com', 1) ON DUPLICATE KEY UPDATE `name`='Tester', `email`='tester@example.com';
Advanced usage:
$command = Yii::app()->db->createCommand(); $command->attachBehavior('InsertUpdateCommandBehavior', new InsertUpdateCommandBehavior); $command->insertUpdate('tbl_user', array( 'name'=>'Tester', 'email'=>'tester@example.com', 'counter'=>'1' ), array( 'name'=>'Tester', 'email'=>'tester@example.com' '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', 'tester@example.com', 1) ON DUPLICATE KEY UPDATE `name`='Tester', `email`='tester@example.com', `counter`=LAST_INSERT_ID(counter);
License
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php