简体   繁体   English

Join语句中的Cake PHP3子查询

[英]Cake PHP3 subquery in Join statement

I need to use cake php method to build this custom query but is that possible to make this sub query in cake stranded way ? 我需要使用cake php方法来构建此自定义查询,但是是否有可能以蛋糕搁浅的方式进行此子查询?

SELECT
                    m.chat_id AS chat_id,
                    m.message AS message,
                    m.created AS created,
                    g.title AS title,
                    u.id AS id,
                    u.name AS name,
                    g.id AS gig_id
                  FROM message m
                  NATURAL JOIN (
                    SELECT   chat_id, MAX(id) AS id
                    FROM     message
                    GROUP BY chat_id
                  ) t
                  RIGHT JOIN chat c ON c.id = t.chat_id
                  INNER JOIN application a ON a.chat_id = m.chat_id
                  INNER JOIN gig g ON g.id = a.gig_id
                  INNER JOIN user u ON u.id = a.user_id
                  WHERE (g.status = 1) 
                  ORDER BY m.created DESC

My code is something like below 我的代码如下

$this->loadModel('Message');
        $dataField = $this->Message->find()
            ->hydrate(false)
            ->select(['Message.chat_id', 'Message.message', 'Message.created', 'u.id', 'u.name', 'g.id'])
            ->join([
                'a' => [
                    'table' => 'application',
                    'type' => 'INNER',
                    'conditions' => 'a.chat_id = chat_id',
                    ],
                'g' => [
                    'table' => 'gig',
                    'type' => 'INNER',
                    'conditions' => 'g.id = a.gig_id',
                    ],
                'u'  => [
                    'table' => 'user',
                    'type' => 'INNER',
                    'conditions' => 'u.id = a.user_id',
                ],

            ])
            ->where(['g.status' => 1])
            ->order(['Message.created' => 'DESC']);

I need to know implement 我需要知道工具

NATURAL JOIN (
                    SELECT   chat_id, MAX(id) AS id
                    FROM     message
                    GROUP BY chat_id
                  ) t
                  RIGHT JOIN chat c ON c.id = t.chat_id

In Cake way. 以蛋糕的方式。 Thanks. 谢谢。

Can you try this? 你可以试试这个吗? Natural Join won't work here. 自然加入在这里不起作用。 I will have a look later though. 我稍后再看。 Thanks. 谢谢。

$this->loadModel('Message');
$query = $this->Message->find()
    ->hydrate(false)
    ->select([
        'Message.chat_id',
        'Message.message',
        'Message.created',
        'u.id',
        'u.name',
        'g.id',
        't.id',
        't.chat_id'
    ])
    ->join([
        'a' => [
            'table'      => 'application',
            'type'       => 'INNER',
            'conditions' => 'a.chat_id = Message.chat_id',
        ],
        'g' => [
            'table'      => 'gig',
            'type'       => 'INNER',
            'conditions' => 'g.id = a.gig_id',
        ],
        'u' => [
            'table'      => 'user',
            'type'       => 'INNER',
            'conditions' => 'u.id = a.user_id',
        ],
        't' => [
            'table'      => '(SELECT chat_id, MAX(id) AS id FROM message GROUP BY chat_id)',
            'type'       => 'INNER',
            'conditions' => 't.id > 0',
        ],
        'c' => [
            'table'      => 'chat',
            'type'       => 'RIGHT',
            'conditions' => 'c.id = t.chat_id',
        ]
    ])
    ->where(['g.status' => 1])
    ->order(['Message.created' => 'DESC']);

$this->loadComponent('Paginator');
$paginated = $this->Paginator->paginate($query, ['limit' => 2]);
pr($paginated);
die;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM