CakePHP 3以表格形式显示相关数据

时间:2017-10-19 05:58:25

标签: php cakephp cakephp-3.x

如何在下面的示例中显示值而不是ID?

表格文件包含: id,title,body,person_id,review_date
我是这样的,但不是在DocumentsController中(如果它很重要):

$test = $this->Documents->find('all',['conditions' => ['review_date'=>date('Y-m-d', strtotime('2022-01-01')) ] ]);

在.ctp中它显示如下:

<tbody>
      <?php foreach ($test as $document): ?>
      <tr>
          <td><?= $document->id;  ?></td>
          <td><?= $document->person_id;  ?></td>
      </tr>
      <?php endforeach; ?>
</tbody>

我想显示人名而不是 person_id 。 所有来自Controller的添加功能:

   public function add()
    {
        $this->loadModel('Documents');
        $this->loadModel('Persons');
        $this->loadModel('SelfReports');
        $this->loadModel('PlannedQuestions');
        $agenda = $this->Agendas->newEntity();
        if ($this->request->is('post')) {
            $agenda = $this->Agendas->patchEntity($agenda, $this->request->getData());
            if ($this->Agendas->save($agenda)) {
                $this->Flash->success(__('The agenda has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The agenda could not be saved. Please, try again.'));
        }
        $per = $this->Persons->find('all');
        $tes = $this->Documents->find('all',[
            'conditions' => ['review_date'=>date('Y-m-d', strtotime('2022-01-01'))], 
        'fields' => ['Documents.id','RefArticles.article_number','Persons.name']]); 
        $selfreports = $this->SelfReports->find('all',['conditions' => ['next_visit_date'=> date( 'Y-m-d', strtotime( 'thursday this week'))]]);
        $plannedquestions = $this->PlannedQuestions->find('list');
        $this->set(compact('agenda', 'tes'));
        $this->set('_serialize', ['agenda']);
    }

1 个答案:

答案 0 :(得分:0)

我自己做... ... 我使用它,它的工作原理是: 在控制器

$tes = $this->Documents->find('all',[
            'contain'=>['Persons'],
            'conditions' => ['review_date'=>date('Y-m-d', strtotime('2022-01-01'))]]);

在.ctp中

 <td><?= $document->person->surnamepat;  ?></td>