【问题标题】:Containable to do show deeper data or join table包含显示更深层次的数据或连接表
【发布时间】:2013-12-23 03:51:43
【问题描述】:

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

【问题讨论】:

    标签: cakephp join pagination cakephp-2.0 containable


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      怎么样

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

      看起来您没有包含“ProjectType”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-29
        • 2021-10-12
        • 1970-01-01
        • 2015-10-12
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多