如何在 Yii 中向关系查询传递参数
我有一个 MANY_MANY 关系:
'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',
'condition'=>'some condiotions AND field_name=:param')
我在 siteController 中得到 Myclass 的结果实例:
$obj->rel
是否可以(以及如何)将 :param 从控制器传递到关系的查询?
I have a MANY_MANY relation:
'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',
'condition'=>'some condiotions AND field_name=:param')
I get the result -instances of Myclass in the siteController:
$obj->rel
Is it possible (and how) to pass the :param from the controller to the relation's query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定这是不可能的,但是你想做的事情可以通过不同的方式实现。
从 指南:
所以你的查询可能是这样的(如果我们想使用急切加载方法):
或者当使用延迟加载方法执行关系查询时:
希望这会有所帮助。请阅读链接的指南。如果需要,请要求澄清。
编辑:
正如下面的评论中已经提到的,
一些条件
可以放入模型的关系中,并且在查询时只需要指定附加条件。附加条件会自动与模型的关系条件进行AND
运算。这似乎与文档相反。无论如何,可以使用以下代码:控制器:
另请参阅 此评论
I'm pretty sure that it's not possible, but what you want to do can be achieved in a different way.
Check the following from the Guide:
So your query could be something like this (if we want to use eager loading approach):
Or when using the lazy loading approach to perform relational query:
Hope this helps. Do read the linked guide. Ask for clarifications, if needed.
Edit:
As already mentioned in the comments below,
some conditions
can be put in the model's relation, and only additional conditions need to be specified while querying. The additional condition is automaticallyAND
'ed to the model's relation condition. This seems contrary to the documentation. Anyway the following code can be used:Controller:
Also see this comment in the linked documentation
您可以尝试“参数化命名范围”:
然后您可以这样使用它:
You can try "Parameterized Named Scopes":
Then you can use it this way: