Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接同一个表以转换为Cakephp标准的Cakephp自定义查询_Php_Mysql_Sql_Cakephp - Fatal编程技术网

连接同一个表以转换为Cakephp标准的Cakephp自定义查询

连接同一个表以转换为Cakephp标准的Cakephp自定义查询,php,mysql,sql,cakephp,Php,Mysql,Sql,Cakephp,我正在为消息传递系统进行如下自定义查询 SELECT m1.mid, users.id AS userid, users.name, users.lname FROM chat_messages AS m1, chat_messages AS m2, users WHERE ( ( m1.user1 = "' . $logged_user . '" AND

我正在为消息传递系统进行如下自定义查询

SELECT m1.mid, 
    users.id AS userid, users.name, users.lname 
    FROM
    chat_messages AS m1,
    chat_messages AS m2,
    users
    WHERE (
             (
                m1.user1 = "' . $logged_user . '"
                AND users.id = m1.user2
                AND m1.user1alldel = "0"
                AND m1.user1allarchive = "0"
             )
             OR (
                m1.user2 = "' . $logged_user . '"
                AND users.id = m1.user1
                AND m1.user2alldel = "0"
                AND m1.user2allarchive = "0"
                )
          )
          AND m1.mid2 = "1"
          AND m2.mid = m1.mid
    GROUP BY m1.mid
    ORDER BY m1.modified DESC
任何人都可以帮助我如何在cakephp自定义分页或通用查找查询中使用此查询


我被客户卡住了,这需要尽快完成。任何帮助都将不胜感激。

您应该查看CakePHP官方文档。它提供了查找和联接表所需的全部知识


对于自定义查询分页
您已经添加了自己的查找方法,例如:聊天用户在您的用户模型中类似:

public $findMethods = array(
        'chatUsers' => true,
    );
$query['fields'] = array('User.id', 'User.lname', '...');
$query['joins'] = array('INNER JOIN chat_messages AS m1...');
$query['conditons'] = array('User.id' => '...');
$query['group'] = array('User.id');
$query['order'] = array('User.id' => 'ASC');
然后创建/删除(在同一模型-用户模型中)您自己的方法/功能\u findChatUsers

protected function _findChatUsers($state, $query, $results = array()) {
        if ($state === 'before') {
                // debug($query); die(); // to see Your query
                // ... modify Your query

           return $query;
        }
        return $results;
    }
阅读有关CakePHP find$params的更多信息: 然后修改查询如下所示:

public $findMethods = array(
        'chatUsers' => true,
    );
$query['fields'] = array('User.id', 'User.lname', '...');
$query['joins'] = array('INNER JOIN chat_messages AS m1...');
$query['conditons'] = array('User.id' => '...');
$query['group'] = array('User.id');
$query['order'] = array('User.id' => 'ASC');
或更快的方式:

        $options = array(
            'fields' => array('...'),
            'joins' => array('...'),
            // ...
        );
        $query = Set::merge($query, $options);
更多关于 ... 最后,在控制器(userscocontroller.php)中检索数据,如下所示:

$this->paginate = array('chatUsers',
    'limit' => 6,
);
$posts = $this->paginate($this->User);
$this->set('posts', $posts);