CakePHP可包含和分页 - 深层关联没有出现

时间:2011-10-03 10:48:02

标签: cakephp cakephp-1.3 containable

我正在尝试在分页查询中使用containsable。代码如下,完整。

    $this->paginate = array(
        'conditions'=>array(
            'Track.pending'=>0,
            'Track.status'=>1
        ),
        'contain'=>array(
            'Vote'=>array(
                'conditions'=>array(
                    'Vote.created >'=>$start,
                    'Vote.created <='=>$end
                ),
                'fields'=>array(
                    'Vote.vote_type_id',
                    'Vote.id',
                ),
            ),
            'Artist'=>array(
                'Image'=>array(
                    'fields'=>array(
                        'Image.name'
                    )
                ),
                'Genre'=>array(
                    'fields'=>array(
                        'Genre.name'
                    )
                ),
                'fields'=>array(
                    'Artist.name',
                    'Artist.id',
                    'Artist.slug',
                    'Artist.city',
                    'Artist.genre_id'
                )
            )
        ),
        'fields'=>array(
            'Track.id',
            'Track.slug',
            'Track.name',
            'Track.featured',
            'Track.plays',
            'Track.vote_score',
            'Track.vote_week_score',
            'Track.video',
            'Track.status',
            'Track.created'
        ),
        'order'=>$order
    );

直接联想(投票和艺术家)被选中,但是图像和流派不是。这是生成的SQL:

SELECT COUNT(*) AS `count` FROM `tracks` AS `Track` LEFT JOIN `artists` AS `Artist` ON (`Track`.`artist_id` = `Artist`.`id`) WHERE `Track`.`pending` = 0 AND `Track`.`status` = 1

SELECT `Track`.`id`, `Track`.`slug`, `Track`.`name`, `Track`.`featured`, `Track`.`plays`, `Track`.`vote_score`, `Track`.`vote_week_score`, `Track`.`video`, `Track`.`status`, `Track`.`created`, `Artist`.`name`, `Artist`.`id`, `Artist`.`slug`, `Artist`.`city`, `Artist`.`genre_id` FROM `tracks` AS `Track` LEFT JOIN `artists` AS `Artist` ON (`Track`.`artist_id` = `Artist`.`id`) WHERE `Track`.`pending` = 0 AND `Track`.`status` = 1 ORDER BY `Track`.`vote_week_score` DESC LIMIT 20

SELECT `Vote`.`vote_type_id`, `Vote`.`id`, `Vote`.`track_id` FROM `votes` AS `Vote` WHERE `Vote`.`created` > '2011-09-26' AND `Vote`.`created` <= '2011-10-03' AND `Vote`.`track_id` IN (24, 35, 31, 25, 27, 34, 56, 58)

蛋糕并未获得更深层次的联想。这是Containable + paginate的限制还是我错过了什么?

谢谢!

3 个答案:

答案 0 :(得分:1)

检查模型的递归属性是否为零值。

$this->Post->recursive = 0;

经过几个小时的头部拆除,上面的线路为我修好了

答案 1 :(得分:0)

确保附上行为:

$this->ModelName->Behaviors->attach('Containable');

答案 2 :(得分:-2)

问题可能是控制器中的$使用,以防你在AppController中设置它。

例:
假设你的控制器是ProductsController,你的模型是Product,你的数据库表是产品。您也可能在模型中设置了某种关联。

如果你没有在Controller中设置$ uses,但是在AppController中设置它,那么Cake会正确地假定数据库表并且工作正常。但是他没有查找你的模型文件,因为它没有出现在$ uses中(在AppController中定义的那个)。

所以如果你在你的控制器中写字它会起作用:

public $uses = array('Yourmodel');

此外:

  1. 您不必设置'递归',这就是包含存在的原因
  2. 不要忘记在包含ex:Product'fields'=&gt; array('category_id')中获取foreignKeys;类别'fields'=&gt;'product_id'