[英]How to write query in CakePHP?
Code: 码:
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
");
I want to write above query in CakePHP. 我想在CakePHP中编写以上查询。 How to write in CakePHP?
如何用CakePHP编写?
Edit: 编辑:
My CakePHP code: 我的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'
)
)
)
)
);
This code is working but not getting the accurate result 该代码有效,但未获得准确的结果
MySQL Code: 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 ;
Probably the problem is that inside your or
statements you have some values without keys, giving you unproper answer. 可能的问题是,在您的
or
语句中您有一些没有键的值,从而给您不正确的答案。
Maybe this conditions will help. 也许这种情况会有所帮助。 Note that I did it based on your mysql query rather than your cake
$conditions
variable: 请注意,我是根据您的mysql查询而不是您的cake
$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
);
Still, it's contradictory that you have conditions from_id = 1
and from_id != 1
尽管如此,您仍然有条件
from_id = 1
和from_id != 1
是矛盾的
If you want make it easy: 如果您想轻松一点:
CakeBook: raw query CakeBook:原始查询
$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;");
But if you want make something more cakeway: 但是,如果您想做更多的事情,请执行以下操作:
In more complex query sometimes you can't make it easy. 在更复杂的查询中,有时您很难做到这一点。 Sometimes you only have to use raw query, or rethink your query.
有时,您只需要使用原始查询或重新考虑您的查询即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.