yii---左查询使用
看到一些做关联查询的示例,例如使用hasMany(一对多),还是有一个hasOne(一对一)的,没有细看,下面是我看到的一个比较好用的一个:
$query = (new \yii\db\Query()) ->select('a.id as id,title,create_time,modify_time,username,name') ->from('yii_article AS a') ->leftJoin('yii_user AS u','u.id = a.user_id') ->leftJoin('yii_category AS c','c.id = a.category_id') ->where(['c.name'=>'新闻分类']) ->limit(4) ->orderBy('id DESC') ->All();
或者是这样:
$command = Admin::find()->select('tbl_user.*,tbl_admin.*')->join('LEFT JOIN','tbl_user','tbl_user.admin_id = tbl_admin.id')->asArray()->all();
具体使用:
$list = (new \yii\db\Query()) ->select('a.content,a.created_time,b.title') ->from('forum_thread_post AS a') ->leftJoin('forum_thread AS b','a.thread_id = b.id') ->where(['a.user_id'=>$this->getUserid()]) ->all();