Tag Archives: Yii Query

Yii createCommand queryAll

I posted here examples of createCommand Query with properties. createCommand Properties are distinct, from, group, having, join, limit, offset, params, pdoStatement, select and where. CDbCommand represents an SQL statement to execute against a database. bindParam used to bind a php variable to parameter in SQL query. bindValue is used to bind a value to and […]

Yii Framework 2 : Delete Query

Model delete() delete() With Condition delete() With Query delete() With Prepared Statements find() And Delete() deleteAll() Model delete() $model = User::find($id); $model->delete(); delete() With Condition $connection->createCommand() ->delete(‘tbl_user’, ‘status = 0’) ->execute(); delete() With Query $connection->createCommand(‘DELETE FROM tbl_user WHERE userid=:userid’) ->execute(); delete() With Prepared Statements Sample 1: $model = $connection->createCommand(‘DELETE FROM tbl_user WHERE userid=:userid’); $model->bindParam(‘:userid’, $userid); […]

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 : Advanced Select Query

Scopes() Scopes Sample 1: class User extends \yii\db\ActiveRecord { namespace app\models; public static function olderThan($query, $age = 5) { $query->andWhere(‘userid > :age’, [‘:age’ => $age]); } } // call the scope function $model = User::find()->olderThan(5)->all(); Sample 2: class User extends \yii\db\ActiveRecord { // … public static function active($query) { $query->andWhere(‘status = 1’); } } // […]

Yii Framework 2 : Select Query For Model

This tutorial will help you to get the list of function to retrieve or select the data from the database using model of yii2.0 framework. I updated most of the functions it will helpful for yii2.0 select query method. find() select() all() one() where() orderBy() count() asArray() indexBy() limit() offset() Limit With Pagination() LIKE Condition […]

Yii Framework 2 : SQL Select Query

findBySql() queryAll() queryOne() queryColumn() queryScalar Query With Prepared Statements() Query Class Note //Before use the Query Class, We have to add the following namespace use yii\db\Query; $connection = \Yii::$app->db; findBySql Sample 1: $sql = ‘SELECT * FROM tbl_user’; $model = User::findBySql($sql)->all(); Sample 2: $sql = ‘SELECT * FROM tbl_user’; $model = User::findBySql($sql)->one(); queryAll $model = $connection->createCommand(‘SELECT […]

Yii Framework 2 : Transaction

Transaction: A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query. beginTransaction() Note //Before use the Query Class, We have to add the following […]

Yii Framework 2 : Update Query

save() OR update() Update Multiple Records update() command Update By Sql Query You can use different type of methods to update the database records in yii2.0 framework. You can use model methods, execute() etc. save() OR update() Using model methods, You can load existing record and update the necessary changes in that. $model = User::find($id); […]