【问题标题】:conditions in contain cakephp find包含 cakephp 的条件
【发布时间】:2013-12-18 22:02:56
【问题描述】:

在CakePHP 中,在查找查询的contain 中设置条件是好是坏,例如:

$data = $this->SomeModel->find('all', array(
    'contain' => array(
        'AnotherModel' => array(
            'conditions' => array(
                // some conditions
            )
        )
    )
));

在哪些情况下将条件放入 contains 是有用的,什么时候应该使用它。抱歉,这仍然让我感到困惑。

谢谢

【问题讨论】:

    标签: php cakephp cakephp-1.3 php-5.3


    【解决方案1】:

    我不确定这是否是一个好主意...首先,我将假设这种特定情况需要您指定此情况独有的条件,即不够笼统,您无法提出将您的SomeModel 模型关系标准添加到AnotherModel

    我的建议是您应该将这些条件放在您的整体查找条件中,因为 contains 指定要返回哪些链接模型(控制引擎盖下的连接)。就像在 SQL 中一样,您可以连接另一个表并在 WHERE 子句中指定要匹配的记录。

    From the manual,您可以在您的容器中指定条件,但它们不会像整体条件那样影响加入您的模型的结果。

    我会这样做:

    $data = $this->SomeModel->find('all', array(
        'contain' => array('AnotherModel'),
        'conditions' => array(
            // some conditions relating to AnotherModel
        )
    ));
    

    【讨论】:

    • 如果您为连接表设置条件,这种方式会返回错误。例如:'conditions' => array('AnotherModel.active'=>1)
    猜你喜欢
    • 2015-04-03
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多