考虑到时间范围,我想获取某个表的行数。
我正在使用 CakePHP 3.7。
在这里你可以看到我的代码(来自控制器类):
public function nbOfDefense($dateIn, $dateOut){
if($dateIn!=null && $dateFin!=null){
$conditions = array('thesis.date_end BETWEEN ? and ?' => array($dateIn, $dateOut));
$query = $this->Thesis->find('all',
array('conditions'=>$conditions));
die(strval($query->count()));
return $query;
}else{
$query = $this->Thesis->find('all');
die(strval($query->count()));
return $query->count();
}
}
我正在使用以下 URL 通过浏览器测试我的功能:
http://localhost:8888/thesis/nbOfDefense/2003-02-01/2019-04-13
我希望我的函数做的是,获取两个日期的参数:
如果这两个日期不为空,则考虑存储在您正在咨询的表中的日期,您将获得两个日期之间的行数。
如果日期为空,则您将获得表的总行数。
并返回一个 int,即这两个日期之间的行数。
我觉得这里的问题是我如何处理我的情况,因为计算总行数非常有效(代码的 else 部分)。
我现在使用此代码的错误如下:
无法将类型的值转换array
为字符串
它指向这条线:
die(strval($query->count()));
我猜count函数返回一个数组(很奇怪,因为当我在没有条件的情况下计算所有行时它不会)。我也试过这个:
die(strval(sizeof($query->count())));
但我得到与以前相同的错误(无法将数组转换为字符串)
我一定错过了什么,但我不知道是什么......