我有以下型号:->
= 1:很多,<->
=多:很多
用户<->
产品->
消息列表<->
消息
我对该查询的目标是查找与已登录相对应的所有消息。我还过滤掉已读取的消息(我在先前的查找中找到了消息ID,并将其存储在$messageIdsReadByPerson
中)。
这是我的尝试:
// only grab messages belonging to this user
$this->Message->unbindModel(array('hasAndBelongsToMany'=>array('MessageListsMessage')));
$this->Message->bindModel( array(
'hasOne' => array(
'MessageListsMessage' => array(
'className' => 'MessageListsMessage',
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array(
'MessageListsMessage.message_id = Message.id'
)
),
'MessageList' => array(
'className' => 'MessageList',
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array(
'MessageList.id = MessageListsMessage.message_list_id'
)
),
'Product' => array(
'className' => 'Product',
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array(
'Product.id = MessageList.product_id'
)
),
'ProductsUser' => array(
'className' => 'ProductsUser',
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array(
'ProductsUser.product_id = Product.id'
)
),
'User' => array(
'className' => 'User',
'foreignKey' => false,
'conditions' => array(
'User.id = ProductsUser.user_id',
'User.id' => $this->Auth->user('id')
)
)
)
));
$messages = $this->Message->find('all', array(
'recursive' => -1
));
$this->set('messages', $messages);
但这仅给出以下查询:
SELECT `Message`.`id`, `Message`.`text`, `Message`.`url` FROM `messages` AS `Message` WHERE 1 = 1
如何使用bindModel(或我不知道的一些更简单的方法,尽管
joins
显然不是可行的方法)正确完成此操作。编辑:
这也不起作用:
function index() {
// only grab messages belonging to this user
$messages = $this->Message->find('all', array(
'contain' => array(
'MessageList' => array(
'Product' => array(
'User.id = ' . $this->Auth->user('id')
)
)
)
));
$this->set('messages', $messages);
}
最佳答案
我认为相同的答案与我在这里给出的相同:
How to get only products with images in CakePhp
可遏制或递归较高
关于mysql - CakePHP:使用bindModel可以向后追溯3个模型吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8826515/