检索 CakePHP 中每行计数的数据

Retrieving data with count for each row in CakePHP

我需要按照标题所说的去做。

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

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

当然是我数据库查询的结果,我要显示出来。我需要在我的站点中表示相同内容。

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

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; ?>

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

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

那你要分组

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

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

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

编辑:以上代码适用于cake2

蛋糕3

只需将字段和组添加到分页器数组中

$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);