Containsable嵌套模型

问题描述:

我有几个嵌套模型,我试图在CakePHP中使用Containable行为加载。 大部分工作正常。Containsable嵌套模型

然而,有一个hasMany关系1个模型只返回1个对象:

$article = $this->News->find('first',array(
    'conditions'=>array('News.id'=>$id), 
    'contain' => array(
     'Newslayout', 
     'Newspicture'=> array(
      'NewspicturesProduct' => array(
       'Product' => array(
        'Brand', 
        'Category' 
       ) 
     ))) 
    )); 

只被加载一次的对象是关系Newspicture hasMany NewspicturesProduct 当我登录查询,我得到如下:

SELECT `NewspicturesProduct`.`id`, `NewspicturesProduct`.`x`, `NewspicturesProduct`.`y`, `NewspicturesProduct`.`product_id`, `NewspicturesProduct`.`newspicture_id`, `NewspicturesProduct`.`w`, `NewspicturesProduct`.`h` FROM `edclondon`.`newspictures_products` AS `NewspicturesProduct` WHERE `NewspicturesProduct`.`newspicture_id` = 3 

哪给了我3结果phpMyAdmin。但只有1 CakePHP的调试:

'Newspicture' => array(
     (int) 0 => array(
      'id' => '3', 
      'news_id' => '2', 
      'newspicture_file_path' => '5022443f-ddf8-4115-ae57-618e9d60b047.jpg', 
      'newspicture_file_size' => '1232546', 
      'order' => null, 
      'NewspicturesProduct' => array(
       'id' => '1', 
       'x' => '0.180664', 
       'y' => '0.295312', 
       'product_id' => '3', 
       'newspicture_id' => '3', 
       'w' => '0.286133', 
       'h' => '0.478125', 
       'Product' => array(
        'id' => '3', 
        //.... 
        'Brand' => array(
         'id' => '6', 
         //... 
        ), 
        'Category' => array(
         'id' => '6', 
         //.... 
        ) 
       ) 
      ) 
     ) 

检索Newspictures对象,而然后检索News对象时,我得到的所有3个NewspicturesProduct对象。

在我看来,那相应的表现,你应该查询代码:

$article = $this->News->find('first',array(
'contain' => array(
    'Newslayout', 
    'Newspicture'=> array(
     'NewspicturesProduct' => array(
      'conditions'=>array('NewspicturesProduct.newspicture_id'=>'3') 
      'Product' => array(
       'Brand', 
       'Category' 
      ) 
    ))) 
)); 

而不是你给了一个...

+0

但你怎么能知道'Newspicture.id'为您回复您检索'News.id' – 2012-08-13 10:14:21

看来你需要从NewspicturesProduct 3条记录。如果那么你可以尝试:

$article = $this->News->find('first',array(
'contain' => array(
    'Newslayout', 
    'Newspicture'=> array(
     'NewspicturesProduct' => array(
      'conditions'=>array(
          'limit'=> 3 
         ), 
      'Product' => array(
       'Brand', 
       'Category' 
      ) 
    ))) 
)); 
+0

由于在相同的查询,但是我有3个做相应记录。但问题是我只得到1.没有那么多,我不想那么多3,而是我想要更多1 – 2012-08-13 10:12:57