doujian0265 2014-03-21 16:09
浏览 15
已采纳

在使用无胖框架过滤后,如何检查记录计数?

I am using fatfree framework with the cortex ORM plugin. I am trying to get the number of records matching a specific criteria. My code:

$group_qry = new \models\system\UserGroup;
$group_qry->load(array('type=?',$name));
echo $group_qry->count(); //always returns 3, i.e total number of records in table

Initially I was thinking that this maybe because the filtering wasn't working and it always fetched everything, but that is not the case, cause I verified it with

while(!$group_qry->dry()){
    echo '<br/>'.$group_qry->type;
    $group_qry->next();
}

So how do I get the number of records actually loaded after filtering?

  • 写回答

2条回答 默认 最新

  • dqkv0603 2014-03-21 16:35
    关注

    Yeah this part is confusing: the count() method actually executes a SELECT COUNT(*) statement. It takes the same arguments as the load() method, so in your case :

    $group_qry->count(array('type=?',$name));
    

    It is not exactly what you need, since it will execute a second SELECT, which will reduce performance.

    What you need is to count the number of rows in the result array. Since this array is a protected variable, you'll need to create a dedicated function for that in the UserGroup class:

    class UserGroup extends \DB\SQL\Mapper {
      function countResults() {
        return count($this->query);
      }
    }
    

    If you feel that it's a bit of overkill for such a simple need, you can file a request to ask for the framework to handle it. Sounds like a reasonable demand.

    UPDATE:

    It's now part of the framework. So calling $group_qry->loaded() will return the number of loaded records.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答