Skip to content
This repository has been archived by the owner on Jul 15, 2020. It is now read-only.

The Yii InsertUpdateCommandBehavior extension adds up some functionality to the default possibilites of yii´s CDbCommand implementation.

License

Notifications You must be signed in to change notification settings

zero-archive/yii-insertupdate-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii InsertUpdateCommandBehavior

Latest Stable Version License

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:

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'=>'[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]';

Advanced usage:

$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);

License

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

About

The Yii InsertUpdateCommandBehavior extension adds up some functionality to the default possibilites of yii´s CDbCommand implementation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages