Tag Archives: Yii Insert

Yii createCommand Insert

Functions function insert($table, $columns) The insert() method builds and executes an INSERT SQL statement. Sample 1 <?php $user=Yii::app()->db->createCommand() ->insert(‘tbl_user’, array(‘username’=>’bsourcecode’,’usertype’=>1,’password’=>’hai’)); ?> OUTPUT SQL INSERT INTO `tbl_user` (`username`, `usertype`,’password’) VALUES (:username,:usertype, :password) Sample 2 <?php $name=’india’; $isactive=1; $command= Yii::app()->db->createCommand( “INSERT INTO regionmaster (`regionname`,`isactive`) VALUES (:name,:isactive)”); $command->bindValue(‘:name’, $name); $command->bindValue(‘:isactive’, $isactive); $sql_result = $command->execute(); ?> OUTPUT SQL INSERT […]

Yii Framework 2 : Insert Query

save() OR insert() insert() command batchInsert() save() OR insert() $model = new User; $model->name = ‘YII’; $model->email = ‘[email protected]’; $model->save(); // equivalent to $model->insert(); insert() command $connection->createCommand()->insert(‘tbl_user’, [ ‘name’ => ‘yii’, ‘status’ => 1, ]) ->execute(); batchInsert() Insert multiple rows at once using batchInsert() function. $connection->createCommand()->batchInsert(‘tbl_user’, [‘name’, ‘status’], [ [‘Bala’, 1], [‘Akilan’, 0], [‘Babu’, 1], […]

Yii Framework 2 : Upsert Query

Upsert is an atomic operation. `Upsert()` will insert new record into a database table If table do not already exist or update them. upsert( $table, $insertColumns, $updateColumns = true, $params = []) Using QueryBuilder $sql = $queryBuilder->upsert(‘pages’, [ ‘name’ => ‘Home page’, ‘url’ => ‘https://bsourcecode.com/’, // url is unique ‘visits’ => 10000, ], [ ‘visits’ […]