The example in this article describes the use of association queries in YII2. Share to everyone for your reference, as follows:
There are two tables, post and category,post.cate_id corresponding category.id
With the GII rise these two tables of model
Then the post model has the following code
Public Function Getcate () { return $this->hasone (Category::classname (), [' id ' = ' cate_id ']);}
At the bottom of the post model, you can get the associated table content by adding the following method:
public static function Getpostsbycategory ($cate _id) { return post::find () ->joinwith (' Cate ') where ([' post.cate_id ' = $cate _id]) ->asarray () ->all ();
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.