Cakephp Complex Relation Find - PullRequest
       9

Cakephp Complex Relation Find

0 голосов
/ 29 октября 2010

У меня есть немного сложная модель базы данных, по которой я не могу найти правильную информацию

пользователь (идентификатор, имя ...)

1 Harsha

2 Jasmine

модулей (id, name)

1 Users

2 Restaurants

3 Dishes

ресторан (id, name ....)

1 KFC 

2 Pizza Hut

блюда (id, name, 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, 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)

Я пытаюсь найти отзывы, сделанные Харшей для всех ресторанов, но не могу ограничить их только отзывами о ресторанах. Я возвращаю Блюда Отзывы также

Вот код, который я использовал // Проверяет, вошел ли пользователь, если да, собирает идентификатор $ 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')))))));

1 Ответ

1 голос
/ 29 октября 2010
    // 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.
...