Skip to main content

Posts

Showing posts from November, 2015

Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView, Gridview with relationships

One of the things you will find tricky to implement is the the sorting and filtering of a GridView's column that displays related model data. As you know if you have been playing with Yii2 lately, there is a new proposed way to search for data and is by using objects that extend from the main entity models and mark the searchable attributes as "safe". So, how do we sort and filter related data on a GridView widget? Lets imagine we have the following relations of a model named "Tour": /* * * @return \yii\db\ActiveQuery */ public function getCountry ( ) { return $this -> hasOne ( Country :: className ( ) , [ ' id ' => ' country_id ' ] ) ; } /* * * @return \yii\db\ActiveQuery */ public function getCity ( ) { return $this -> hasOne ( City :: className ( ) , [ ' id ' => ' city_id ' ] ) ; } And we wish to display the name of the country and the name of the city on a GridView. To do

Yii2 query / query builder: how to use OR condition

Yii2 query / query builder: how to use OR condition You can use some thing like this: $third_res_count =CustomerReservedTimeslots:: find () ->select([ 'COUNT(timeslot_id) AS cnt' ]) ->where([ 'test_date' => $today_date , 'confirmed' => '1' , ]) ->andWhere( [ '>' , 'third_user_id' , '0' ]) ->andFilterWhere([ 'or' , [ 'like' , 'status' , 'checked in' ], [ 'like' , 'status' , 'Waiting for check in' ], ]) ->one();

YII2 DB Count Results query

YII2 DB count results query Simple in the controller or view file add do this: $count =MODEL:: find ()   ->select([ 'COUNT(id) AS cnt' ])   ->where([   'field1' => $value1 , ])-> andWhere([ '>' , 'field2' , $value2 ]) - >one()->cnt; And in the model of the table add before this line: public static function tableName() Add public $cnt ; like below: public $cnt ; public static function tableName() {