Containable to do show deeper data or join table

399 views Asked by At

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' => '[email protected]',
        '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');
2

There are 2 answers

0
XuDing On

How about

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

Looks like you did not contain "ProjectType"

0
Kai On

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.