以下连接结构是否适用于CakePHP查找?

时间:2013-08-29 19:05:16

标签: mysql cakephp search join

以下连接结构是否正确格式化以使用CakePHP查找?当我将它与条件一起使用时,这似乎不起作用,我的直觉告诉我结构已关闭。我是新手加入,所以任何帮助都表示赞赏。

'joins' => array(
    (int) 0 => array(
        'table' => 'cheeses_milk_sources',
        'alias' => 'CheesesMilkSource',
        'type' => 'INNER',
        'conditions' => array(
            (int) 0 => 'Cheese.id = CheesesMilkSource.cheese_id'
        )
    ),
    (int) 1 => array(
        'table' => 'milk_sources',
        'alias' => 'MilkSource',
        'type' => 'INNER',
        'conditions' => array(
            (int) 0 => 'CheesesMilkSource.milk_source_id = MilkSource.id',
            'CheesesMilkSource.milk_source_id' => '1'
        )
    )
),
'conditions' => array(
    'AND' => array(
        (int) 0 => array(
            'Cheese.cheese_producer_id' => (int) 35
        ),
        (int) 1 => array(
            'Cheese.active' => (int) 1
        )
    )
),

1 个答案:

答案 0 :(得分:1)

令人困惑的部分是

    'conditions' => array(
        (int) 0 => 'CheesesMilkSource.milk_source_id = MilkSource.id',
        'CheesesMilkSource.milk_source_id' => '1'
    )

您正在混合没有键的数组元素和一个键等于CheesesMilkSource.milk_source_id的数组元素。

如果您需要在连接上指定两个条件,请执行

    'conditions' => array(
        'CheesesMilkSource.milk_source_id = MilkSource.id AND CheesesMilkSource.milk_source_id = 1'
    )

也许下一个片段同样有效,但我不确定,现在也无法测试 - 让我知道是否有评论。

    'conditions' => array(
        'CheesesMilkSource.milk_source_id = MilkSource.id',
        'CheesesMilkSource.milk_source_id = 1'
    )

但是既然你要加入表格,也许你应该把CheesesMilkSource.milk_source_id = 1放在查找的一般条件中:

'conditions' => array(
   'Cheese.cheese_producer_id' => 35,
   'Cheese.active' => 1,
   'CheesesMilkSource.milk_source_id' => 1,
)

请注意,您无需指定AND,因为这是加入条件的默认方式。