CakePHP:对递归查询的分页不按预期工作

时间:2012-09-18 12:55:16

标签: cakephp pagination recursive-query

我有这个型号:

class CompanyRestaurantRequest extends AppModel
{
    public $name = "CompanyRestaurantRequest";      
    //public $actsAs = array('Containable');        
    public $hasMany = array(
        "UserRestaurantRequestFeed" => array(
            'dependent' => true
        )
    );
}

class UserRestaurantRequestFeed extends AppModel{
    public $name = "UserRestaurantRequestFeed";         
    public $belongsTo = array("CompanyRestaurantRequest");
    public $hasMany = array("UserRestaurantRequestFeedResponse");
}

class UserRestaurantRequestFeedResponse extends AppModel{
    public $name = "UserRestaurantRequestFeedResponse";

    public $belongsTo = array("UserRestaurantRequestFeed");

}

我试图像这样分页CompanyRestaurantRequest:

$this->CompanyRestaurantRequest->recursive = 5;
$this->CompanyRestaurantRequestFeed->recursive = 5;
$this->paginate = array(
    'limit' => 10,
    'order' => array(
             'CompanyRestaurantRequest.id' => 'desc'
    )
);
$this->set('requests', $this->paginate("CompanyRestaurantRequest"));

问题是我没有得到CompanyRestaurantRequestFeedResponse。

我尝试将ContainableBehavior附加到CompanyRestaurantRequest模型,并在分页设置中使用 contains 键,如下所示:

'contain' => array('CompanyRestaurantRequestFeedResponse');

我尝试的任何东西,我都无法获得与CompanyRestaurantRequestFeeds相关的那些CompanyRestaurantRequestFeedResponses。

我怎样才能让它发挥作用?

谢谢!

1 个答案:

答案 0 :(得分:0)

而不是行:

$this->paginate = array(
  'limit' => 10,
  'order' => array(
         'CompanyRestaurantRequest.id' => 'desc'
  )
);

在Controller的顶部,声明所有var的地址为:

public $paginate = array(
  'CompanyRestaurantRequest' => array(
    'limit' => 10,
    'order' => array(
      'CompanyRestaurantRequest.id' => 'desc'
    )
  )
);

这应该解决它......