【问题标题】:find('count') keep getting total no. of rows instead of the value 0find('count') 不断得到总数。行数而不是值 0
【发布时间】:2013-01-24 01:32:16
【问题描述】:

为什么我总是得到总数。当我输入“”时,行数而不是值 0 - 空?

控制器

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

查看

<?php echo $totalSchools ?>

【问题讨论】:

    标签: php cakephp cakephp-1.3 cakephp-1.2


    【解决方案1】:

    你应该首先过滤你的$searchQuery,如果它是空的,就像这里一样:

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

    【讨论】:

    • 因为如果$searchQuery 是空的,你最终会像count(*) 一样在你的表中得到所有东西
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      相关资源
      最近更新 更多