检索CakePHP中每行的计数数据

时间:2017-10-24 02:52:55

标签: php cakephp cakephp-3.0

我需要做标题所说的。

我有一个特定的查询,我必须显示。 我不想做原始查询。我想像我做的任何其他查询一样。

这是我需要在索引中显示的结果:

Click here

当然是我必须显示的数据库查询的结果。我需要在我的网站上代表相同的内容。

以下方法就是我所做的。但我不知道如何做计数的部分(这是我完成查询所需的唯一部分):

public function index()
{
    $country= $this -> Auth -> User()['country_fk'];
    $this->paginate['contain'] = ['Dogs','Dogs.Human','Dogs.Human.Cities','Users'];
    $this->paginate['conditions'] = ['Users.isHuman' => 0, 'Cities.country_fk' => $country];
    $this->paginate['order'] = array('Badges.id' => 'desc');
    $badges= $this->paginate($this->Badges);
    $this->set(compact('badges'));
    $this->set('_serialize', ['badges']);
}

一些例子进行查询然后进行计数,但我认为不是我正在寻找的。

非常感谢。

修改 在arilia回答之后,在我的控制器中,我有以下几行:

public function index()
{
    $country= $this -> Auth -> User()['country_fk'];
    $this->Turnos->virtualFields = array('count' => 'COUNT(*)');
    $this->paginate['contain'] = ['Dogs','Dogs.Human','Dogs.Human.Cities','Users'];
    $this->paginate['conditions'] = ['Users.isHuman' => 0, 'Cities.country_fk' => $country];

     $this->paginate['group'] = array('Badges.value2');
     $this->paginate['order'] = array('Badges.value2' => 'desc');

     $badges= $this->paginate($this->Badges);
     $this->set(compact('badges'));
     $this->set('_serialize', ['badges']);
}

这就是我展示价值观所做的:

<thead>
        <tr>
            <th><?= $this->Paginator->sort('idBadge', 'ID') ?></th>
            <th><?= $this->Paginator->sort('name', 'Name') ?></th>
            <th><?= $this->Paginator->sort('value1', 'Value 1') ?></th>
            <th><?= $this->Paginator->sort('value2', 'Value 2') ?></th>
            <th><?= $this->Paginator->sort('count', 'Quantity') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($badges as $badge): ?>
        <tr>
            <td><?= h($badge->idBadge) ?></td>
            <td><?= h($badge->name) ?></td>
            <td><?= h($badge->value1) ?></td>
            <td><?= h($badge->value2) ?></td>
            <td><?= h($badge->count) ?></td>
        </tr>
    <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

首先,您应该创建一个虚拟字段

$this->Badges->virtualFields['count'] = 'COUNT(*)';

然后你必须分组

$this->paginate['group'] = array('the field you want to group by');

但是你必须告诉paginator组件你想要查询中的那个字段

$this->paginate['fields'] = array('count', ...);

编辑:上面的代码是针对cake2的

对于蛋糕3

只需将字段和组添加到paginator数组

即可
$this->paginate['fields'] = 
[
    'count' => 'COUNT(*)'
    // other fields you want to retrieve go here
];

$this->paginate['group'] = ['the field you want to group by'];

无论如何,我会以这种方式使用查询构建器

 $badges = $this->Badges->find()
     ->select(['count' => 'COUNT(*)'])
     ->select($this->Badges) // this command tells the query builder to    
                             // select all the fields of badges table
                             // other than count
     ->select($this->Badges->Dogs) 
     ->select($this->Badges->Dogs->Humans) 
     ->select(... and so on for every model you want ...)
     ->contain(['Dogs','Dogs.Human','Dogs.Human.Cities','Users'])
     ->where(['Users.isHuman' => 0, 'Cities.country_fk' => $country])
     ->group(['the field to group'])
     ->order(['Badges.id' => 'desc']);

$badges= $this->paginate(badges);