gpt4 book ai didi

php - Cakephp 复杂关系查找

转载 作者:行者123 更新时间:2023-11-29 14:58:01 25 4
gpt4 key购买 nike

我有一个有点复杂的数据库模型,我无法在其中进行正确的查找

用户(ID、姓名...)

1 Harsha

2 Jasmine

模块(id、名称)

1 Users

2 Restaurants

3 Dishes

餐厅(id、名称...)

1 KFC 

2 Pizza Hut

菜肴(id、名称、restaurant_id ..)

1 Cheese Pizza 2

项目(id、module_id、item_id)

1 1 1 (refers to User Harsha)

2 2 1 (Refers to KFC)

3 2 2 (Refers to Pizza Hut)

4 1 2 (Refers to User Jasmine)

5 3 1 (Refers to Cheese Pizza)

评论(id、parent_id、消息、item_id、commenters_id)

1 0 "I love the ambience of Pizza Hut" 3 1 (Refers to Harsha reviewing Pizza Hut)

2 1 "You remind me of that kid in the next table who freaked me out." 3 2 (Refers to Jasmine Replying to Harsha's review on Pizza Hut)

3 0 "I love Cheese Pizza in Pizza Hut" 5 1 (Refers to the Cheese Pizza Review by Harsha)

我正在尝试查找 Harsha 对所有餐厅的评论,但无法将其仅限于仅对餐厅的评论。我也收到了菜肴评论

这是我使用过的代码 //检查用户是否登录,如果是则收集 ID $id = $this->_loggedIN();

    // Find the ItemID from the Item Table
$itemId = $this->User->Item->itemId('1', $id);

// Finding the User Data and last Status Message
$user = $this->User->Item->find('first', array('conditions' => array('Item.id' => $itemId), 'contain' => array('User', 'StatusMessage' => array('limit' => 1, 'order' => 'StatusMessage.created DESC'))));

// Find the Restaurant Reviews of the Current User
$reviews = $this->User->Item->Review->find('all', array('conditions' => array('Review.commenters_item_id' => $itemId, 'Review.pid = 0'), 'order' => array('Review.created DESC'),
'contain' => array(
'Item' => array(
'User' => array(
'fields' => array('id', 'first_name', 'last_name', 'username', 'file')),
'Restaurant' => array('fields' => array('id', 'name', 'slug', 'file', 'area_id', 'city_id'),
'Area')),
'ReviewReply' => array(
'Item' => array(
'User' => array(
'fields' => array('id', 'first_name', 'last_name', 'username', 'file')))))));

最佳答案

    // I find it clearer to do the contain like this

$this->Review->contain(array('Item','Item.User','Item.Restaurant', 'ReviewReply' etc... ));

// Again I find it clearer this way as it avoids long lines and deeply indented arrays.

$conditions = array(
'Review.commenters_item_id' => $itemId,
'Review.pid' => 0, // should this be parent_id?
'Item.module_id' => 2
);
$this->Review->find('all', array('conditions' => $conditions, 'order'=>
//...
//etc.

关于php - Cakephp 复杂关系查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047179/

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