سؤال

لدي نموذج قاعدة بيانات معقدًا صغيرًا لا أستطيع العثور عليه

المستخدم (معرف ، الاسم ...)

1 Harsha

2 Jasmine

الوحدات النمطية (معرف ، الاسم)

1 Users

2 Restaurants

3 Dishes

مطعم (معرف ، اسم ....)

1 KFC 

2 Pizza Hut

أطباق (معرف ، اسم ، مطعم ...)

1 Cheese Pizza 2

العناصر (المعرف ، الوحدة النمطية ، 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 ، المعلقين)

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 لجميع المطاعم ولكني غير قادر على تقييدها في مراجعات المطاعم وحدها. سأعود إلى مراجعات الطبق أيضًا

فيما يلي الرمز الذي استخدمته // الشيكات إذا تم تسجيل الدخول إلى المستخدم إذا قامت بنعم بإعداد المعرف $ = $ 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.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top