【问题标题】:Containable Nested models可包含的嵌套模型
【发布时间】:2012-08-10 15:16:43
【问题描述】:

我尝试使用 CakePHP 中的 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 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

这在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 对象。

【问题讨论】:

    标签: cakephp cakephp-2.0 containable


    【解决方案1】:

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

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

    而不是你给的那个......

    【讨论】:

    • 但是你怎么知道Newspicture.id在你检索News.id的同一个查询中
    【解决方案2】:

    您似乎需要来自NewspicturesProduct 的 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条
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多