可以包含显示更深层数据或连接表

时间:2013-12-05 23:30:09

标签: cakephp join pagination cakephp-2.0 containable

我有3个表:projects,project_reminder_users,project_types。

The relations is as follow:
Project => belong to => ProjectType
           hasMany   => ProjectReminderUser

ProjectReminderUser => belong to => Project

ProjectType => hasMany   => Project

我根据这个

分配的人(ProjectReminderUser)得到所有数据
$this->Project->ProjectReminderUser->Behaviors->load('Containable');
$this->paginate = array(
    'ProjectReminderUser' => array(                     
        'limit' => $limit,
        'contain' => array(
                        'Project' => array(
                            'ProjectComment',
                            'ProjectFile',
                        ),
                        'User'
                    ),
        'conditions' => array(
            'User.group_id' => $this->Session->read('Auth.User.group_id'),
            'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id'),
            'Project.project_status_id' => PROJECT_STATUS_OPEN,

        ),  
        'order' => 'Project.due_date',          
    )
);

$this->set('myTasks', $this->paginate('ProjectReminderUser'));  

,结果看起来像这样

array(
    'ProjectReminderUser' => array(
        'id' => '96',
        'user_id' => '1',
        'project_id' => '46'
    ),
    'Project' => array(
        'id' => '46',
        'project_type_id' => '9',
        'contact_id' => null,
        'company_id' => null,
        'subject' => 'Test Modified Field',
        'description' => 'Test Modified Field',
        'ProjectFile' => array(
            (int) 0 => array(
                'id' => '19',
                'project_id' => '46',
                'user_id' => '6',
                'file_path' => '46_bhbinary_xmm_1728.jpg',
                'notes' => null,
                'created' => '2013-11-26 18:37:49'
            ),
        ),
        'ProjectComment' => array(
        )
    ),
    'User' => array(
        'password' => '*****',
        'id' => '1',
        'group_id' => '1',
        'email' => 'xxx@xxxxx.com',
        'first_name' => 'xxxx',
        'deleted' => false,
        'displayName' => 'xxxxx'
    )
)

在结果中有Project.project_type_id的数据,但我想了解更多细节。所以我可以将其显示为名称而不是数字。也许像ProjectType.name。 我怎样才能做到这一点,所以我可以在视图中对它进行排序?像这样的东西

$this->Paginator->sort('ProjectType.name', 'Type');

2 个答案:

答案 0 :(得分:0)

问题在于Paginator与深层模型关联不起作用。我认为,如果不是使用Cake的方法进行模型关联,而是手动进行连接,您可以根据需要计算排序。请参阅以下讨论。

http://sinkpoint.railsplayground.net/cakephp-pagination-deep-sort-and-habtm/

最糟糕的是,你可能还需要重写模型的paginate函数来处理你需要它的排序。

答案 1 :(得分:0)

怎么样

'contain' => array(
                        'Project' => array(
                            'ProjectComment',
                            'ProjectFile',
                            'ProjectType',
                        ),
                        'User'
                    ),

看起来你没有包含“ProjectType”