silverstripe 查询不起作用,最佳调试方法?
silverstripe query does not work, best way to debug?
我的这个 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());
对于开始 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/
正确答案是不使用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());
正如其他答案已经指定的那样,不要在 return 上使用 = (或在 return 前面放置 $ 使其成为变量),并且 return 查询本身使用 $datalist->sql()
http://api.silverstripe.org/3.1/class-DataList.html#_sql
但是 - 查看有关 SQLQuery 的文档是错误的,因为您没有使用 SQLQuery。您使用的是 ORM,因此此文档页面更相关:http://docs.silverstripe.org/en/3.1/developer_guides/model/data_model_and_orm/#filterany
我的这个 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());
对于开始 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/
正确答案是不使用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());
正如其他答案已经指定的那样,不要在 return 上使用 = (或在 return 前面放置 $ 使其成为变量),并且 return 查询本身使用 $datalist->sql()
http://api.silverstripe.org/3.1/class-DataList.html#_sql
但是 - 查看有关 SQLQuery 的文档是错误的,因为您没有使用 SQLQuery。您使用的是 ORM,因此此文档页面更相关:http://docs.silverstripe.org/en/3.1/developer_guides/model/data_model_and_orm/#filterany