Cakephp 可包含嵌套模型
我有一些嵌套模型,我正试图使用CakePHP中的Cakephp 可包含嵌套模型,cakephp,cakephp-2.0,containable,Cakephp,Cakephp 2.0,Containable,我有一些嵌套模型,我正试图使用CakePHP中的Containable行为加载它们。 大部分都很好用 但是,具有hasMany关系的1个模型只返回1个对象: $article = $this->News->find('first',array( 'conditions'=>array('News.id'=>$id), 'contain' => array( 'Newslayout', 'Newspicture'=&
Containable
行为加载它们。
大部分都很好用
但是,具有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有许多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
这在phpMyAdmin
中给出了3个结果。但CakePHP调试中只有1个:
'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'
)
)))
));
而不是您给出的…在我看来,您显示的查询对应的代码应该是:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array('NewspicturesProduct.newspicture_id'=>'3')
'Product' => array(
'Brand',
'Category'
)
)))
));
而不是你给的那张…看来你需要3张来自
NewspicturesProduct
的记录。如果是这样,您可以尝试:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array(
'limit'=> 3
),
'Product' => array(
'Brand',
'Category'
)
)))
));
您似乎需要从
新闻图片产品
中获得3条记录。如果是这样,您可以尝试:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array(
'limit'=> 3
),
'Product' => array(
'Brand',
'Category'
)
)))
));
谢谢你的回复,不过我有3条对应的记录,但问题是我只得到1条。不是说我不想要超过3条,而是我想要超过1条感谢你的回复,但是我有3条对应的记录,但问题是我只得到1条。与其说我不想要超过3个,不如说我想要超过1个,但在检索
News.id
的同一查询中,您如何知道newscient.id
,但在检索News.id
的同一查询中,您如何知道newscient.id