gpt4 book ai didi

php - Yii2 Activerecord hasMany 与左连接

转载 作者:行者123 更新时间:2023-11-30 23:46:45 25 4
gpt4 key购买 nike

在使用 Yii2 的 Active Record 时遇到一些问题。

我想设置一个包含左连接的 hasMany 关系,但是这样做时记录只填充了一个关系对象。

这是当前代码 - 将执行的查询从 Yii 调试复制粘贴到 navicat 中肯定会返回多个结果。

public function getx()
{

return $this->hasMany(x::className(),['x' => 'x'])
->viaTable('a', ['arId' => 'arId'], function ($query){


$query->leftJoin('a b', 'a.type = b.type AND a.val < b.val')
->andWhere('a.foo IN (0,1,2,3)')
->andWhere('a.bar IN ((0,1,2,3)')
->andWhere('a.moo IN ((0,1,2,3)')
->andWhere('b.val IS NULL');
}
);

}

但是下面的代码工作正常(假设查询不同)。

    return $this->hasMany(x::className(),['x' => 'x'])
->viaTable('a', ['arId' => 'arId'], function ($query){

$query->andWhere(['and', ['foo' => [0,1,2,3]],
['bar' => [0,1,2,3]],
['moo' => [0,1,2,3]],
]
);
}
);

它在 Controller 中被调用如下:

$rs = ar::find(1)->with('x')->all();

问题显然是联接 - 谁能告诉我为什么会这样?

最佳答案

请试试这个,因为这个对我有用

public function getx()
{

return $this->hasMany(x::className(), ['xId' => 'xId'])
->leftJoin('a b', 'a.type = b.type AND a.val < b.val',
function($query){
'..here you can add more join'
})
->andWhere('a.foo IN (0,1,2,3)')
->andWhere('a.bar IN ((0,1,2,3)')
->andWhere('a.moo IN ((0,1,2,3)')
->andWhere('b.val IS NULL');

}

如果这有任何问题,请告诉我,我再说一遍,它对我有用。

关于php - Yii2 Activerecord hasMany 与左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33356358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com