我有这个silverstripe查询不起作用(它输出所有消息而不是具有日期范围的消息)
解决此查询的最佳方法是什么? 我对silverstripe相当新,并且无法找到有关如何打印原始查询的信息。
return = Message::get()
->filter(array(
'IsPublished' => true,
'StartPublication:LessThanOrEqual' => date('Y-m-d'),
'Priority' => array('High', 'Normal')
))
->where("\"StopPublication\" >= ".date('Y-m-d')." OR \"StopPublication\" IS NULL")
->sort('StartPublication', 'DESC')->limit($this->getLimit());
答案 0 :(得分:2)
正确的答案是不使用where() - 这是很多学习者陷入的陷阱方法(可能是由于名称)。它基本上只适用于ORM无法处理的非常复杂的事情。
你至少在调用过滤器,这是正确的。但是你想要的是而不是where()是filterAny():
Message::get()
->filter([
'IsPublished' => true,
'StartPublication:LessThanOrEqual' => 'now',
'Priority' => ['High', 'Normal']
])
->filterAny([
'StopPublication:GreaterThanOrEqual' => 'now',
'StopPublication' => null
])
->sort('StartPublication', 'DESC')
->limit($this->getLimit());
正如另一个答案已经指定的那样,不要在返回时使用=(或者在返回前面放一个$以使其成为变量),并返回查询本身使用$datalist->sql()
{{3} }
但是 - 看到SQLQuery上的文档是错误的,因为你没有使用SQLQuery。您正在使用ORM,因此此文档页面更具相关性:http://api.silverstripe.org/3.1/class-DataList.html#_sql
答案 1 :(得分:1)
对于启动return = Message::get()
,它只是return Message::get()
我假设您已设置php错误报告,以便输出错误,SS也处于开发模式,因此它不会隐藏错误输出。
你的问题的答案是要做到:
将其输出到输出html:
Debug::dump(Message::get()
->filter(array(
'IsPublished' => true,
'StartPublication:LessThanOrEqual' => date('Y-m-d'),
'Priority' => array('High', 'Normal')
))
->where("\"StopPublication\" >= ".date('Y-m-d')." OR \"StopPublication\" IS NULL")
->sort('StartPublication', 'DESC')->limit($this->getLimit())->sql());
或将其输出到项目根目录文件
Debug::log(Message::get()
->filter(array(
'IsPublished' => true,
'StartPublication:LessThanOrEqual' => date('Y-m-d'),
'Priority' => array('High', 'Normal')
))
->where("\"StopPublication\" >= ".date('Y-m-d')." OR \"StopPublication\" IS NULL")
->sort('StartPublication', 'DESC')->limit($this->getLimit())->sql());
请参阅http://docs.silverstripe.org/en/developer_guides/model/sql_query/