开发者

Cakephp Complex Relation Find

I have a little complex Database model on which i am not able to make a proper find

user(id, name...)

1 Harsha

2 Jasmine

modules (id, name )

1 Users

2 Restaurants

3 Dishes

restaurant (id, name.... )

1 KFC 

2 Pizza Hut

dishes (id, name, restaurant_id .. )

1 Cheese Pizza 2

items (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)

reviews (id, parent_id, message, 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)

i am trying to find the reviews done by Harsha of all the restaurants but am not able to limit them to the reviews of Restaurants alone. I am getting back the Dish Reviews also

Here is the code i have used // Checks if the User is logged in if yes gathers the 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'))));

   开发者_如何学C // 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜