查找(“计数”)不断获得总编号.行数,而不是值 0


find('count') keep getting total no. of rows instead of the value 0

为什么当我输入"时,我总是得到行的总编号而不是值0 - 空?

控制器

$totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
$this->set('totalSchools', $totalSchools);

视图

<?php echo $totalSchools ?>
如果

$searchQuery为空,您应该先过滤它,如下所示:

if($searchQuery != ""){
      $totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
       $this->set('totalSchools', $totalSchools);
}
    else
        $this->set('totalSchools', 0);

因为空字符串的计算结果为 '%%' =>所以这匹配所有内容。您必须使用 if/else 检查此特殊情况。