Reputation: 6562
I've a very simple problem with related tables in CakePhp. I've got two tables with a many to many through relationship.
I simply want to display the name of the related object, not it's id.
My interface made by the console is exactly the same as this one:
I found the recursion parameter in the CakePhp docs, I found many related questions on StackOverflow, and yet still failed.
Here is the Controller:
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
$this->Application->id = $id;
if (!$this->Application->exists()) {
throw new NotFoundException(__('Invalid application'));
}
$this->set('application', $this->Application->read(null, $id));
}
Here is the view.
<div class="related">
<h3><?php echo __('Related Application Memberships'); ?></h3>
<?php if (!empty($application['ApplicationMembership'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Id'); ?></th>
<th><?php echo __('Chantier Id'); ?></th>
<th><?php echo __('Application Id'); ?></th>
<th><?php echo __('Ponderation'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php
$i = 0;
foreach ($application['ApplicationMembership'] as $applicationMembership): ?>
<tr>
<td><?php echo $applicationMembership['id']; ?></td>
<td><?php echo $applicationMembership['chantier_id']; ?></td>
<td><?php echo $applicationMembership['application_id']; ?></td>
<td><?php echo $applicationMembership['ponderation']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('controller' => 'application_memberships', 'action' => 'view', $applicationMembership['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('controller' => 'application_memberships', 'action' => 'edit', $applicationMembership['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('controller' => 'application_memberships', 'action' => 'delete', $applicationMembership['id']), null, __('Are you sure you want to delete # %s?', $applicationMembership['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Upvotes: 0
Views: 2093
Reputation: 21
If you are in 2015, trying to figure it out a solution for this at cakephp3 here something that may help:
Tables-> People, Patients, Drugs, drugs_patients Patient has person_id drugs_patients is a many to many between Drugs and Patients, and has other fields like startDate and endDate
Now you are at the drugsPatients controller, in the add, and you want to populate the select patient with the id from patients and the name from people.
It's piece of cake:
$patients = $this->DrugsPatients->Patients->find('list', ['keyField' => 'id','valueField' => 'person.name'])->contain(['People']);
=)
Upvotes: 1
Reputation: 29141
Use CakePHP's Containable Behavior. There are tutorials/explanations EVERYWHERE including at least 500 or so I've written myself here on StackOverflow.
Basically, CakePHP's Containable Behavior lets you pick and choose anything/everything you want returned in the data, as long as the associations are set up correctly.
Read about Containable, and try it. If it still doesn't work, post that as a "Can't get containable to work in CakePHP" question (or something like that)
Upvotes: 1