Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2中的多对多关系查询_Php_Yii_Yii2 - Fatal编程技术网

Php Yii2中的多对多关系查询

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

如何使用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.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])->所有()
你好,谢谢你的帮助,但是有没有什么方法可以让我得到一个包含object
app\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']);
}