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>
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)