2

我有一些嵌套模型,我正在尝试使用ContainableCakePHP 中的行为加载它们。大部分工作正常。

但是,具有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。但是在 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对象。

4

2 回答 2

0

在我看来,与您显示的查询对应的代码应该是:

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

而不是你给的那个...

于 2012-08-10T17:19:40.840 回答
-1

看来您需要 3 条记录NewspicturesProduct。如果是这样,那么您可以尝试:

$article = $this->News->find('first',array(
'contain' =>  array(
    'Newslayout', 
    'Newspicture'=> array(
        'NewspicturesProduct' => array(
            'conditions'=>array(
                             'limit'=> 3
                         ),
            'Product' => array(
                'Brand',
                'Category'
            )
    )))
));
于 2012-08-12T09:42:10.957 回答