当我需要在模型中使用相关数据时,我将一对键(id_item
和 id
),查询结果如下------。
... ON (category.id = category_translate.id_item) ...
public function getTranslate()
{
return $this->hasMany(CategoryTranslate::className(),
['id_item' => 'id'])->indexBy('language');
}
但如果我需要 ON
查询的部分内容是这样的 category.left between 1 and 10
.
我如何能做到这一点?
你不应该有一个查询条件在ON部分。ON部分应该是连接查询表到你加入的表。
实际上你想做的是,过滤你的数据。但过滤与关系无关。所以,你应该保持你的关系的原样(也许甚至可以去掉你的关系中的 indexBy('language')
部分!),然后像这样执行你的查询。
$categories = Category::find()
->andWhere(['between', 'left', 1, 10])
->joinWith('translate')
->indexBy('language');