Yii Insert Tutorials

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' => 'http://bsourcecode.com/', // url is unique 'visits' => 10000, ], [ 'visits' => new \yii\db\Expression('visits + 1'), ], $params = array()); Using Command: Yii::$app->db->createCommand()->upsert('pages', [ 'name' => […]... Read More »

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 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 INSERT INTO regionmaster (`regionname`,`isactive`) VALUES (:name,:isactive)... Read More »