我正在使用CakePHP 3.4+
我有三张桌子
和协会
table_a
hasMany table_b
(table_b
包含外键table_a_id
) table_b
belongsTo table_status
(table_b
包含外键table_status_id
) 现在,我从包含table_a
和table_b
table_status
抓取记录
$records = $this->TableA->find()
->where([
'TableA.user_id' => $this->Auth->user('id'),
'TableA.deleted' => false,
])
->contain([
'TableB.TableStatus',
]);
在template
文件中,我循环遍历$records
<tr>
<td>Title</td>
<td>Record count</td>
</tr>
<?php foreach($records as $record): ?>
<tr>
<td><?= $record->title ?></td>
<td><?= count($record->table_b) ?>
</tr>
<?php endforeach; ?>
输出为
+---------+--------------+
| Title | Record Count |
+---------+--------------+
| T1 | 5 |
+---------+--------------+
| T2 | 8 |
+---------+--------------+
table_status
有三个字段failed
,pending
,complete
。
我想以格式
显示Record Count
列
<count_failed>/<count_pending>/<count_complete>
eg., 1/4/3 ## where 1 => count_failed, 4 => count_pending, 3 => count_complete
如何在查询中包含count AS
以根据每条记录的相关模型创建三个计数变量?