如何在CakePHP中编写查询?

时间:2014-05-30 13:51:24

标签: php cakephp

代码:

mysql_query("
    SELECT
        message,to_id,from_id,MAX(created) AS created
    FROM
        ".MEMBER_MSG."
    WHERE
            (to_id='".$uid."' OR from_id='".$uid."')
        AND
            from_id != '".$uid."'
        AND
            (to_delete='1' OR from_delete='1')
        AND
            is_chat='1'
    GROUP BY
        from_id,to_id
    ORDER by
        created DESC
");

我想在CakePHP中编写上面的查询。如何用CakePHP写?

修改

我的CakePHP代码:

$condition = array(
    'OR' => array(
        'AND' => array(
            'Message.from_id' => $this->Session->read('Auth.User.id'),
            'Message.to_id' => $this->Session->read('Auth.User.id')
        ),
        array(
            'AND' => array(
                'Message.from_id' => $userid['User']['id'],
                'Message.to_id' => $this->Session->read('Auth.User.id')
            )
        )
    ),
    'is_chat' => '1',
    array(
        'OR' => array(
            'AND' => array(
                'Message.from_id' => $this->Session->read('Auth.User.id'),
                'Message.from_delete' => '1'
            ),
            array(
                'AND' => array(
                    'Message.to_id' => $this->Session->read('Auth.User.id'),
                    'Message.to_delete' => '1'
                )
            )
        )
    )
);

此代码正常运行但未获得准确结果

MySQL代码:

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `from_id` int(11) NOT NULL,
  `to_id` int(11) NOT NULL,
  `message` text NOT NULL,
  `is_view` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0-Not Viewed, 1-Viewed',
  `is_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0=No, 1=Yes',
  `is_chat` tinyint(4) NOT NULL COMMENT '0=No, 1=Yes',
  `from_delete` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0-Deleted by From, 1-Not',
  `to_delete` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0-Deleted by To, 1-Not',
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `reply_count` smallint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `from_id` (`from_id`),
  KEY `to_id` (`to_id`),
  KEY `from_delete` (`from_delete`),
  KEY `to_delete` (`to_delete`),
  KEY `is_view` (`is_view`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2 个答案:

答案 0 :(得分:2)

问题可能是你的or语句中有一些没有键的值,给你一个不正确的答案。

也许这种情况会有所帮助。请注意,我是基于你的mysql查询而不是你的蛋糕$conditions变量:

$conditions = array(
    'OR' => array(
        'to_id' => $uid,
        'from_id' => $uid
    ),
    'from_id !=' => $uid,
    'OR' => array(
        'to_delete' => 1,
        'from_delete' => 1
    ),
    'is_chat' => 1
);

但是,你有条件from_id = 1from_id != 1

是矛盾的

答案 1 :(得分:-1)

如果你想轻松一点:

CakeBook: raw query

$this->Message->query("SELECT message,to_id,from_id,MAX(created) AS created FROM ".MEMBER_MSG." WHERE (to_id='".$uid."' OR from_id='".$uid."') AND from_id != '".$uid."' AND (to_delete='1' OR from_delete='1') AND is_chat='1' GROUP BY from_id,to_id ORDER by created DESC;");

但是如果你想要更多的东西,那么:

CakeBook: find

在更复杂的查询中,有时候你无法轻松完成。有时您只需使用原始查询,或重新考虑您的查询。