Php Yii2中的多对多关系查询
如何使用ActiveQueryInterface方法在Yii 2中编写上述查询Php Yii2中的多对多关系查询,php,yii,yii2,Php,Yii,Yii2,如何使用ActiveQueryInterface方法在Yii 2中编写上述查询 如果您使用的是ActiveRecords,则可以在内容类中创建如下关系: $data = (new \yii\db\Query()) ->select('c.*') ->from('content c') ->innerJoin('contentTags ct', 'c.id = ct.content') ->innerJoin('tags t', 'ct.ta
如果您使用的是ActiveRecords,则可以在内容类中创建如下关系:
$data = (new \yii\db\Query())
->select('c.*')
->from('content c')
->innerJoin('contentTags ct', 'c.id = ct.content')
->innerJoin('tags t', 'ct.tag = c.id')
->where(['t.id' => 1])
->all();
$query=new\yii\db\query()$查询->选择(['c.]]->from(“{{content}c”)->innerJoin({{contentTags}}ct”,“c.id=ct.content”)->innerJoin({tags}}t”,“ct.tag=c.id”)->其中(['=',t.id',1])->所有()
你好,谢谢你的帮助,但是有没有什么方法可以让我得到一个包含objectapp\model\Content
数组的结果,就像我们从Content::find()->all()中喷射一样代码>
$data = (new \yii\db\Query())
->select('c.*')
->from('content c')
->innerJoin('contentTags ct', 'c.id = ct.content')
->innerJoin('tags t', 'ct.tag = c.id')
->where(['t.id' => 1])
->all();
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag'])
->viaTable('contentTags', ['content' => 'id']);
}