我有一个 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 从 Controller 传递给关系的查询?
最佳答案
我很确定这是不可能的,但您可以通过不同的方式实现您想要做的事情。
从the Guide中检查以下内容:
We can use dynamic relational query options in both with() and the with option. The dynamic options will overwrite existing options as specified in the relations() method. ...
所以您的查询可能是这样的(如果我们想使用预加载方法):
$param='something';
$obj=SomeModel::model()->with(array(
'rel'=>array('condition'=>'some conditions AND field_name=:param',
'params' => array(':param' => $param))
))->findAll();
// some more code
$obj->rel->attributeOne;
或者当使用延迟加载方式执行关系查询时:
$param='something';
$obj=SomeModel::model()->findByPk(1);
$rels=$obj->rel(array('condition'=>'some conditions AND field_name=:param',
'params' => array(':param' => $param)
));
希望这对您有所帮助。请阅读链接的指南。如果需要,要求澄清。
编辑:
正如下面的评论中已经提到的,一些条件
可以放在模型的关系中,查询时只需要指定额外的条件。附加条件自动 AND
到模型的关系条件。这似乎与文档相反。无论如何,可以使用以下代码:
// In the model's relation:
'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',
'condition'=>'some conditions');
Controller :
$param='something';
$obj=SomeModel::model()->with(array(
'rel'=>array('condition'=>'field_name=:param',
'params' => array(':param' => $param))
))->findAll();
另见 this comment在链接文档中
关于php - 如何在 Yii 中将参数传递给关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9396191/