2017-10-19 159 views
0

如何在下面的示例中顯示值而不是ID?CakePHP 3以表格形式顯示相關數據

表文件包含: ID,標題,正文,爲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。 所有的控制器添加功能:

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']); 
    } 

回答

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>