php - How to use active query method of another model class via relation in Yii2 -


i have models below

/**  * @property integer $id  * @property string $title  */ class book extends activerecord {     public static function find() {         return new bookquery(self::class);     } }  class bookquery extends activequery {     public function andwheretitlelike($string) {         return $this->andwhere(['like', 'title', $string]);     } }  /**  * @property integer $id  * @property integer $userid  * @property integer $bookid  * @property integer $addedtime  */ class favoritebook extends activerecord {      public function getbook() {         return $this->hasone(book::class, ['id' => 'bookid']);      } } 

querying favorite books of current logged user below

$dataprovider = new activedataprovider([     'query' => favoritebook::find()                    ->andwhere(['userid' => yii::$app->user->id]) ]); 

but how query favorite book of user contains title reusing method in bookquery above?

try "joinwith" following way

favoritebook::find() ->joinwith(book)     ->andwhere(['userid' => yii::$app->user->id]) 

to make sure working can array, print record , make confirm following.

$fav_book = favoritebook::find() ->joinwith(book)     ->andwhere(['userid' => yii::$app->user->id]) ->asarray()->all();  var_dump($fav_book); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -