CakePHP 3 在表单中显示关联数据
CakePHP 3 Display associated data in form
如何在下面的示例中显示值而不是 ID?
Table 文档包含:
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。
所有从控制器添加功能:
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']);
}
我自己做...
我用这个并且它有效:
在控制器中
$tes = $this->Documents->find('all',[
'contain'=>['Persons'],
'conditions' => ['review_date'=>date('Y-m-d', strtotime('2022-01-01'))]]);
在 .ctp 中
<td><?= $document->person->surnamepat; ?></td>
如何在下面的示例中显示值而不是 ID?
Table 文档包含:
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。 所有从控制器添加功能:
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']);
}
我自己做...
我用这个并且它有效:
在控制器中
$tes = $this->Documents->find('all',[
'contain'=>['Persons'],
'conditions' => ['review_date'=>date('Y-m-d', strtotime('2022-01-01'))]]);
在 .ctp 中
<td><?= $document->person->surnamepat; ?></td>