gpt4 book ai didi

cakephp 包含关联条件问题

转载 作者:行者123 更新时间:2023-12-02 22:24:32 25 4
gpt4 key购买 nike

我有以下联想

后->小学->中学

$results = $this->Post->find('all', array(
'conditions' => array(
'Post.post_id =' => 2,
'Primary.secondary_id !=' => null
),
'contain' => array(
'Primary' => array(
'Secondary' => array(
'conditions' => array('Secondary.short_code =' => 'code')
)
)
)
));

返回这个。

Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2
[created] => 2012-10-29 09:48:29
[modified] => 2012-10-29 09:48:29
)

[Primary] => Array
(
[id] => 3
[secondary_id] => 6
[Secondary] => Array
(
[id] => 6
[short_code] => code
[created] => 2012-10-31 11:19:56
[modified] => 2012-10-31 11:20:03
)

)

)

但是当我改变

'conditions' => array('Secondary.short_code =' => 'code')

'conditions' => array('Secondary.short_code !=' => 'code')

当我不希望它返回时,它仍然返回主记录。

Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2
[created] => 2012-10-29 09:48:29
[modified] => 2012-10-29 09:48:29
)

[Primary] => Array
(
[id] => 3
[secondary_id] => 6
[Secondary] => Array
(
)

)

)

最佳答案

很难确切地知道您希望实现什么,但我认为这听起来像是您试图根据针对“次要”模型的条件来限制“主要”结果。

如果是这种情况,您将需要使用 joins() 而不是 contain()

原因:当你使用CakePHP's Containable Behavior ,它实际上做单独的查询,然后在将数据返回给你之前组合结果。这样做在很多方面都很棒,但它不允许您根据不利于其子项的条件限制父项结果。

为此,只需使用 joins() . (创建 MySQL JOIN 的 CakePHP 语法)

关于cakephp 包含关联条件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com