0

I'm using CakePHP 3.4+

I have three tables

  1. table_a
  2. table_b
  3. table_status

and association are

  1. table_a hasMany table_b (table_b contain foreign key table_a_id)
  2. table_b belongsTo table_status (table_b contain foreign key table_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?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

0 Answers0