如何在 Yii 中向关系查询传递参数

发布于 2025-01-08 15:08:20 字数 312 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2025-01-15 15:08:20

我很确定这是不可能的,但是你想做的事情可以通过不同的方式实现。
指南

我们可以在 with() 和 with 选项中使用动态关系查询选项。动态选项将覆盖在 transactions() 方法中指定的现有选项。 ...

所以你的查询可能是这样的(如果我们想使用急切加载方法):

$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');

控制器:

$param='something';
$obj=SomeModel::model()->with(array(
'rel'=>array('condition'=>'field_name=:param',
             'params' => array(':param' => $param))
))->findAll();

另请参阅 此评论

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:

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. ...

So your query could be something like this (if we want to use eager loading approach):

$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;

Or when using the lazy loading approach to perform relational query:

$param='something';
$obj=SomeModel::model()->findByPk(1);
$rels=$obj->rel(array('condition'=>'some conditions AND field_name=:param',
                      'params' => array(':param' => $param)
));

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 automatically AND 'ed to the model's relation condition. This seems contrary to the documentation. Anyway the following code can be used:

// 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();

Also see this comment in the linked documentation

﹂绝世的画 2025-01-15 15:08:20

您可以尝试“参数化命名范围”:

function relations() {
    return array(
        'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',  
             'condition'=>'some conditions')
    )
}


public function relByFieldName($fieldValue=5)
{
    $this->getDbCriteria()->mergeWith(array(
        'with' => 'rel',
        'condition' => 'field_name = :value',
        'params' => array(':value' => $fieldValue)
    ));
    return $this;
}

然后您可以这样使用它:

$models=Model::model()->relByFieldName(100)->findAll();

You can try "Parameterized Named Scopes":

function relations() {
    return array(
        'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',  
             'condition'=>'some conditions')
    )
}


public function relByFieldName($fieldValue=5)
{
    $this->getDbCriteria()->mergeWith(array(
        'with' => 'rel',
        'condition' => 'field_name = :value',
        'params' => array(':value' => $fieldValue)
    ));
    return $this;
}

Then you can use it this way:

$models=Model::model()->relByFieldName(100)->findAll();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文