0

我想使用一个复杂的查询paginate()。这是我的代码:

// PhasesController.php
public function manage($id = null) {
    $this->loadModel('ProjectDomainPhasesUser');
    $this->ProjectDomainPhasesUser->recursive = 0;
    $this->paginate = array(
      'ProjectDomainPhasesUser' => array(
      'order' => array('Phase.label_'.$_SESSION['lang']),
      'joins' => array(
         array(
            'table' => 'users',
            'alias' => 'User',
            'type' => 'INNER',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectDomainPhasesUser.user_id = User.id AND User.id = '.$this->Auth->User('id')
            )
         ),
         array(
            'table' => 'project_domains_phases',
            'alias' => 'ProjectDomainsPhase',
            'type' => 'INNER',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectDomainsPhase.id = ProjectDomainPhasesUser.project_domain_phase_id'
            )
         ),
         array(
            'table' => 'processes_phases',
            'alias' => 'ProcessesPhase',
            'type' => 'LEFT',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectDomainsPhase.process_phase_id = ProcessesPhase.id'
            )
         ),
         array(
            'table' => 'phases',
            'alias' => 'Phase',
            'type' => 'LEFT',
            'foreignKey' => false,
            'conditions'=> array(
             'ProcessesPhase.phase_id = Phase.id'
            )
         ),
         array(
            'table' => 'projects_domains',
            'alias' => 'ProjectsDomain',
            'type' => 'LEFT',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectDomainsPhase.project_domain_id = ProjectsDomain.id'
            )
         ),
         array(
            'table' => 'domains',
            'alias' => 'Domain',
            'type' => 'LEFT',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectsDomain.domain_id = Domain.id'
            )
         ),
         array(
            'table' => 'projects',
            'alias' => 'Project',
            'type' => 'LEFT',
            'foreignKey' => false,
            'conditions'=> array(
             'ProjectsDomain.project_id = Project.id'
            )
         )
      ),
      'fields' => array('Project.title', 'Project.date_target', 'ProcessesPhase.step_number', 'Domain.label_fr AS domain_fr', 'Domain.label_es AS domain_es', 'Phase.label_fr AS phase_fr', 'Phase.label_es AS phase_es', 'ProjectDomainsPhase.date_notification', 'ProjectDomainsPhase.done', 'ProjectDomainsPhase.date_finished', 'ProjectDomainsPhase.id')
      )
    );
    $this->set('taches', $this->paginate());

我通过关注cakephp 做到了这一点:使用连接搜索结果

我的观点 :

// manage.ctp
<table cellpadding="8" cellspacing="0">
<tr>
    <th><?php echo $this->Paginator->sort('Project.title','Projet'); ?></th>
    <th><?php echo $this->Paginator->sort('Project.date_target','Date cible du projet'); ?></th>
    <th><?php echo $this->Paginator->sort('ProcessesPhase.step_number','Etape'); ?></th>
    <th><?php echo $this->Paginator->sort('Domain.domain_'.$_SESSION['lang'],'Domaine'); ?></th>
    <th><?php echo $this->Paginator->sort('Phase.phase_'.$_SESSION['lang'],'Tâche'); ?></th>
    <th><?php echo $this->Paginator->sort('ProjectDomainsPhase.date_notification','Date notification'); ?></th>
    <th><?php echo __('Statut'); ?></th>
    <th><?php echo $this->Paginator->sort('ProjectDomainsPhase.date_finished','Date fin tâche'); ?></th>
</tr>
<?php
foreach ($taches as $key => $tache){ ?>
<tr>
    <td><?php echo $tache['Project']['title']; ?>&nbsp;</td>
    <td><?php echo $this->Date->date_inv($tache['Project']['date_target']); ?>&nbsp;</td>
    <td><?php echo $tache['ProcessesPhase']['step_number']; ?>&nbsp;</td>
    <td><?php echo $tache['Domain']['domain_'.$_SESSION['lang']]; ?>&nbsp;</td>
    <td><?php echo $tache['Phase']['phase_'.$_SESSION['lang']]; ?>&nbsp;</td>
    <td><?php echo $this->Date->date_inv($tache['ProjectDomainsPhase']['date_notification']); ?>&nbsp;</td>
    <td><?php 
    echo '<input type="hidden" name="data['.$key.'][ProjectDomainsPhase][done]" value=0 />';
    echo '<input type="checkbox" name="data['.$key.'][ProjectDomainsPhase][done]" value="1"';
    if ($tache['ProjectDomainsPhase']['done']==1)
    echo ' checked';
    echo ' />';
    echo '<input type="hidden" name="data['.$key.'][ProjectDomainsPhase][id]" value='.$tache['ProjectDomainsPhase']['id'].' />';
    ?>&nbsp;</td>
    <td><?php echo $this->Date->date_inv($tache['ProjectDomainsPhase']['date_finished']); ?>&nbsp;</td>
</tr>
<?php };
?>
</table>

我的问题是,在我看来,所有数据都很棒,但是当我尝试排序时,通过单击一列,页面重新加载但没有任何变化。

4

1 回答 1

0

我有一个新的解决方案..

其实我也面临同样的问题,终于找到了一个技巧来做到这一点..

这是我的加入查询和功能..

        $fields = array('SubjectCategory.name,Subject.name');
        $joins = array();
        $joins[] = array(
            'table' => 'subject_category_link',
            'foreignKey' => false,
            'conditions' => array('SubjectCategoryLink.subject_id = Subject.subject_id'
            ),
            'type' => 'INNER',
            'alias' => 'SubjectCategoryLink',
        );
        $joins[] = array(
            'table' => 'subject_category',
            'foreignKey' => false,
            'conditions' => 'SubjectCategory.subject_category_id = SubjectCategoryLink.subject_category_id',
            'type' => 'INNER',
            'alias' => 'SubjectCategory',
        );
        $conditions = array('SubjectCategory.parent_category_id' => NULL);
        $limit = 10;
        $group = 'Subject.name';
        $this->paginate = array('Subject' => compact('conditions', 'fields', 'order', 'limit', 'joins', 'group'));
        $order='';
       /* Here you should solution code provided bellow */

        $this->paginate('Subject');

当我在视图文件中调用波纹管代码时,cakephp 分页器组件仅对上述记录进行排序。

echo $this->Paginator->sort('Subject.name');

我面临的问题是我无法通过单击视图文件中生成的链接按 SubjectCategory.name 对记录进行排序。

echo $this->Paginator->sort('SubjectCategory.name');

但它仅在初始加载时通过在控制器中定义 $order 才起作用..

$order = 'SubjectCategory.name asc';

所以我分析了 cakephp 中的分页器组件(位于 app/Cake/Controller/Component/PaginatorComponent.php 中),并获得了对记录进行排序的技巧..

1.在控制器中的分页调用 ($this->paginate('Subject')) 之前添加此部分..

    if (array_key_exists('sort', $this->params['named'])) {
        $order = $this->params['named']['sort'] . ' ' . $this->params['named']['direction'];
    } else {
        $order = 'Subject.name asc';
    }
    $this->Session->write('paginate_custom',1);
    $this->paginate = array('Subject' => compact('conditions', 'fields', 'order', 'limit', 'joins', 'group'));

    $this->set(compact('order'));// Dont forget to set this order variable to view
  1. 去编辑 cakephp 分页器组件。(app/Cake/Controller/Component/PaginatorComponent.php)

在第 141 行,您可以在 paginate() 函数中看到以下代码。

 $options = $this->mergeOptions($object->alias);

在此行之后,您添加以下逻辑。

    if ($this->Controller->Session->read('paginate_custom')) {
        unset($options['sort']);
        unset($options['direction']);
        $this->Controller->Session->delete('paginate_custom');
    }

3.在您看来,在每次排序调用时替换下面的代码($this->Paginator->sort('SubjectCategory.name') )

    <td><?php
         $col_string = explode(' ', $order);
         $col = $col_string[0];
         if ($col == 'SubjectCategory.name') {
             $dir = ($col_string[1] == 'asc') ? 'desc' : 'asc';
         } else {
             $dir = 'asc';
         }
         echo $this->Paginator->sort('SubjectCategory.name', 'Name', array('direction' => $dir));?>
    </td>

我想这可能会帮助你..

一切顺利..

快乐编程..

于 2014-02-15T13:34:10.923 回答