I'm using CakePHP 3.4+
I have three tables
- table_a
- table_b
- table_status
and association are
table_a
hasManytable_b
(table_b
contain foreign keytable_a_id
)table_b
belongsTotable_status
(table_b
contain foreign keytable_status_id
)
Now, I'm fetching records from table_a
containing table_b
and table_status
$records = $this->TableA->find()
->where([
'TableA.user_id' => $this->Auth->user('id'),
'TableA.deleted' => false,
])
->contain([
'TableB.TableStatus',
]);
in the template
file, I'm looping over $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; ?>
which output as
+---------+--------------+
| Title | Record Count |
+---------+--------------+
| T1 | 5 |
+---------+--------------+
| T2 | 8 |
+---------+--------------+
table_status
has three fields failed
, pending
, complete
.
I want to display Record Count
column in format
<count_failed>/<count_pending>/<count_complete>
eg., 1/4/3 ## where 1 => count_failed, 4 => count_pending, 3 => count_complete
How to include count AS
in the query to create three variables of count based on associated model for each record?