在CakePHP 3

时间:2018-01-18 11:24:27

标签: cakephp cakephp-3.4 cakephp-model

我正在使用CakePHP 3.4+

我有三张桌子

  1. 表-A
  2. 表-B
  3. table_status
  4. 和协会

    1. table_a hasMany table_b table_b包含外键table_a_id
    2. table_b belongsTo table_status table_b包含外键table_status_id
    3. 现在,我从包含table_atable_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有三个字段failedpendingcomplete

      我想以格式

      显示Record Count
      <count_failed>/<count_pending>/<count_complete>
      eg., 1/4/3       ## where 1 => count_failed, 4 => count_pending, 3 => count_complete
      

      如何在查询中包含count AS以根据每条记录的相关模型创建三个计数变量?

0 个答案:

没有答案