Tag Archives: Yii Model

CDbCriteria In Yii

CDbCriteria is the one of best support in yii framework. CDbCriteria is used to assign the values or property in query. Using cdbcriteria property we can assign condition, order, limit, scopes etc for query. It have some methods to apply condition for model or sql table like addCondition(), addInCondition() etc . alias Condition Distinct Group […]

Yii 1.0 Model Structure

By default model classes are available within the protected/models dirctory. Every class must be inherit from the CModel or subclass. Yii difines two sub classes. They are CActiveRecord, CFormModel. CActiveRecord is conntected with database table model (classes) and it represent the table structure. CFormModel is the basic model class and conntected to HTML forms. Class Extend CFormModel Structure Class Extend CActiveRecord […]

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 : 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); […]