Containable to do show deeper data or join table

橙三吉。 提交于 2019-12-13 04:37:51

问题


I have 3 tables: projects, project_reminder_users, project_types.

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

ProjectReminderUser => belong to => Project

ProjectType => hasMany   => Project

I get all data based on who assigned(ProjectReminderUser) by this

$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'));  

and the result look like this

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'
    )
)

In the result There is data for Project.project_type_id, but I would like to have more detail of that. So I can show it as name instead of number. maybe like ProjectType.name instead. How can I achieve this, so I can sort it in the view? something like this

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

回答1:


The problem is that Paginator doesn't play nice with deep model associations. I think though if rather than using Cake's methods for doing model associations, you instead do your joins manually, you may be able to work out the sorting as you want. See the below discussion.

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

Worst comes to worse, you may have to also rewrite the model's paginate function to deal with sorting how you need it to.




回答2:


How about

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

Looks like you did not contain "ProjectType"



来源:https://stackoverflow.com/questions/20412999/containable-to-do-show-deeper-data-or-join-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!