2012-09-27 84 views
0

我有兩個模型名稱圖像和評論。這裏的關係就像圖像有很多評論。現在在我的列表頁面中,我想要顯示所有圖像細節,並僅顯示該圖像上的評論數量。你能打電話給我嗎?我應該怎麼做? i之後編寫查詢我的返回數據是cakephp 2.0 hasmany關係檢索數據

` 陣列 (

[0] => Array 
    (
     [Image] => Array 
      (
       [image_id] => 57 
       [user_id] => 1 
       [category_id] => 22 
       [image_title] => scroul 
       [description] => beutifull natural image for the animal 
       [keyword] => scrual 
       [image_price] => 
       [image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg 
       [image_path] => img/uploads/images/original/ 
       [image_available_size] => 
       [like] => 12 
       [size] => 3244 
       [resolution] => 2162 x 1644 
       [i_date] => 1348573022 
       [i_by] => 1 
       [u_date] => 1348573022 
       [u_by] => 1 
       [is_active] => Y 
       [is_deleted] => N 
      ) 

     [Comment] => Array 
      (
       [0] => Array 
        (
         [comment_id] => 5 
         [image_id] => 57 
         [user_id] => 2 
         [comment] => socute 
         [comment_date] => 1348739230 
         [is_active] => N 
         [is_deleted] => N 
        ) 

      ) 

    ) 

[1] => Array 
    (
     [Image] => Array 
      (
       [image_id] => 56 
       [user_id] => 1 
       [category_id] => 22 
       [image_title] => cute dog 
       [description] => cute dog looking 
       [keyword] => 
       [image_price] => 
       [image_name] => d4af899b0d52cccbec94952a3abd0077.jpg 
       [image_path] => img/uploads/images/original/ 
       [image_available_size] => 
       [like] => 8 
       [size] => 620 
       [resolution] => 2592 x 1944 
       [i_date] => 1348572897 
       [i_by] => 1 
       [u_date] => 1348572897 
       [u_by] => 1 
       [is_active] => Y 
       [is_deleted] => N 
      ) 

     [Comment] => Array 
      (
       [0] => Array 
        (
         [comment_id] => 3 
         [image_id] => 56 
         [user_id] => 2 
         [comment] => ohhhhhhh 
         [comment_date] => 1348737968 
         [is_active] => N 
         [is_deleted] => N 
        ) 

      ) 

    ) 

[3] => Array 
    (
     [Image] => Array 
      (
       [image_id] => 55 
       [user_id] => 1 
       [category_id] => 22 
       [image_title] => ships 
       [description] => ships with beutiful green background 
       [keyword] => ship,green,animal,nature,background,eating,white ship 
       [image_price] => 
       [image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg 
       [image_path] => img/uploads/images/original/ 
       [image_available_size] => 
       [like] => 1 
       [size] => 1831 
       [resolution] => 2520 x 1944 
       [i_date] => 1348572846 
       [i_by] => 1 
       [u_date] => 1348661976 
       [u_by] => 1 
       [is_active] => Y 
       [is_deleted] => N 
      ) 

     [Comment] => Array 
      (
       [0] => Array 
        (
         [comment_id] => 2 
         [image_id] => 55 
         [user_id] => 2 
         [comment] => i like it 
         [comment_date] => 1348737942 
         [is_active] => Y 
         [is_deleted] => N 
        ) 

       [1] => Array 
        (
         [comment_id] => 4 
         [image_id] => 55 
         [user_id] => 2 
         [comment] => good scene 
         [comment_date] => 1348738004 
         [is_active] => N 
         [is_deleted] => N 
        ) 

      ) 

    ) 

`上述陣列中 有該圖像的所有評論。我不想在這裏評論列表我只想要沒有評論。 在此先感謝。

回答

1

您可以發佈您正在使用的查找語句嗎?您可以設置遞歸到一定的水平,從文檔:

  • -1蛋糕只獲取Group的數據,沒有加入。
  • 0蛋糕取出組數據,並且其領域
  • 1蛋糕取一組,其領域及其相關聯的用戶
  • 2蛋糕取一組,其結構域,其相關聯的用戶,與用戶的相關聯的文章

所以,你可以撥打以下,如果你只是只從當前模型所需的數據:

$this->Model->find('all', array('recursive' => -1)); 

也有Containable行爲,它允許您在調用find時指定要檢索的模型數據。

假設你有一個郵政模型hasMany圖片。呼叫,與中郵型號的中容納的行爲適當地包括,將是:

$this->Post->find('all', array(
'conditions' => array('Post.id' => 1), 
'contain' => array('Image') 
)); 

編輯: 因爲拼寫和格式我誤解你最初的問題的。我以爲你想「沒有評論」出現在數據陣列中,而不是你想要的「評論數量」出現。

如果您想要評論數量,請使用Kishor Kundan提議的counterCache

1

蛋糕提供了另一個驚人的魔術,「counterCache」。

您可以在註釋模型中定義的counterCache

public $belongsTo = array(
    'className' => 'Image', 
    'foreignKey' => <your_foreign_key>, 
    ... 
    ... 
    'counterCache' => true 

); 

然後你在你的圖像表中添加一個字段,「COMMENT_COUNT」(或正在使用該模型圖像的數據表)和將濾餅爲你做剩下的事。

這確實會在每次添加/刪除評論時增加開銷,但是與每次提取圖像數據時發佈「計數」相比,它更好。

欲瞭解更多信息,你可以檢查the cookbook。在那裏尋找「counterCache」。

UPDATE:

要限制計數器緩存範圍,使用額外的屬性「counterScope」作爲

public $belongsTo = array(
    'className' => 'Image', 
    'foreignKey' => <your_foreign_key>, 
    ... 
    ... 
    'counterCache' => true, 
    'counterScope' => array('Image.active' => 1) 

); 
+0

其對我很有幫助,但還是我有一個問題。在我的項目中,如果用戶添加評論,默認情況下其deactive,然後也在圖像主comment_count價值將增加1,我不想這樣。當管理員使該評論活躍,然後我想增加comment_count。你可以幫我嗎 ? –