0

我有 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');
4

2 回答 2

0

问题是 Paginator 不能很好地处理深度模型关联。我认为,如果不是使用 Cake 的方法进行模型关联,而是手动进行连接,则可以根据需要进行排序。请参阅下面的讨论。

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

最糟糕的是,您可能还必须重写模型的分页函数来处理您需要的排序方式。

于 2013-12-05T23:40:33.713 回答
0

怎么样

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

看起来您没有包含“ProjectType”

于 2013-12-06T03:58:18.357 回答