将MySql查询转换为Cakephp 2.6

时间:2016-10-20 06:28:48

标签: mysql cakephp cakephp-2.6

朋友您好我是Cakephp 2.6的新手,我需要将MySql查询转换为Cakephp 2.6。我正在获得所需的输出,我通过任务ID获取客户端和顾问组之间的最后一次对话。我不想编写Model:query(),因为我也需要cakephp的默认分页。

这是我的下表,请看看

enter image description here

SELECT * FROM (SELECT task_id, MAX(created) AS created FROM task_conversations GROUP BY task_id) AS x 
JOIN task_conversations USING (task_id, created) 
WHERE (client_id=3 OR consultant_id= 3) ORDER BY modified ASC

这是以上查询的以下输出:

enter image description here

我试图通过编写以下代码行来解决它

$this->paginate = array(
            'conditions' => array(
                'TaskConversation.sender_id'    => $consultant['User']['id'],
                'Task.status'                       => 1
            ),
            'group'         => 'TaskConversation.task_id',
            'limit'         => 3,
            'order'         => array('TaskConversation.conversation'=> 'ASC'
            ),
            'recursive'     => 2
        );
        $this->set('tasks', $this->Paginator->paginate());

我也会在这里要求分页。请告诉我您的反馈意见。提前致谢

1 个答案:

答案 0 :(得分:0)

最后我得到了解决方案。这是解决方案:

我在Model中添加了自定义分页功能。这是代码:

public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = []) {    


     if(isset($extra['isCustomPagination']) && $extra['isCustomPagination']==0) {
                $recursive = -1;

    return $this->find(
        'all',
        compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive')
    );
            }else{
                $recursive = -1;
        // Mandatory to have
        $this->useTable = false;
        $sql = '';
        $sql .= "(SELECT * FROM (SELECT task_id, MAX(created) AS created FROM task_conversations GROUP BY task_id) AS Task JOIN task_conversations as TaskConversation USING (task_id, created) INNER JOIN tasks as Task2 ON Task2.id=Task.task_id INNER JOIN users as Client ON TaskConversation.client_id=Client.id INNER JOIN users AS Consultant ON TaskConversation.consultant_id = Consultant.id where (TaskConversation.consultant_id = ".$extra['extra']['consultant_id'].") order by TaskConversation.modified ASC) ";

        // Adding LIMIT Clause
        $sql .= ' LIMIT '.(($page - 1) * $limit) . ', ' . $limit;
        $results = $this->query($sql);
        return $results;
            }
    }

    public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {

      if(isset($extra['isCustomPagination']) && $extra['isCustomPagination']==0) {

                    $parameters = compact('conditions');
      if ($recursive != $this->recursive) {
    $parameters['recursive'] = $recursive;
   }
   $count = $this->find('count', array_merge($parameters, $extra));
   return $count;
             }
             else {
                 $sql = '';
        $sql .= "(SELECT * FROM (SELECT task_id, MAX(created) AS created FROM task_conversations GROUP BY task_id) AS Task JOIN task_conversations as TaskConversation USING (task_id, created) INNER JOIN tasks as Task2 ON Task2.id=Task.task_id INNER JOIN users as Client ON TaskConversation.client_id=Client.id INNER JOIN users AS Consultant ON TaskConversation.consultant_id = Consultant.id where (TaskConversation.consultant_id = ".$extra['extra']['consultant_id'].") order by TaskConversation.modified ASC) ";
        $this->recursive = $recursive;
        $results = $this->query($sql);
        return count($results);
             }
    }

在控制器中我添加了以下几行:

$this->paginate = array('TaskConversation'=>array('limit'=>2,'extra'=>array('client_id'=>$consultant['User']['id'],'consultant_id'=>$consultant['User']['id'])));

如果您不想在与相同型号相关的其他功能中使用自定义分页,那么我已经在分页中传递了这个#iscustomPagination' => 0.

$this->TaskConversation->contain('Task','Client','Consultant');
         $this->paginate = array(
            'conditions' => array(
                'TaskConversation.task_id'  => $id,
                'Task.status'               => 1
            ),
            'order'         => array('TaskConversation.created'=> 'ASC'
            ),
            'recursive'     => 2,
            'isCustomPagination' => 0,
         );
        $this->set('conversations', $this->Paginator->paginate());