I have two tables in my app: ‘Clients’ and ‘Appointments’
In the appointments table I have a foreign key called ‘clientid’ which links the tables together. How would I show the appointments for that user?
So for example when viewing a client /admin/clients/view/201/ I would see the Client details for client 201 and also the appointments for that client.
So far my controller looks like this:
class ClientsController extends AppController
{
var $name = 'Clients';
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('*'));
}
function admin_view($id = null)
{
$this->Client->id = $id;
$this->set('client', $this->Client->read());
}
Any help would be much appreciated. Thanks
Edit: Client model
class Client extends AppModel
{
var $name = 'Client';
var $useTable = 'clients';
}
Edit2: View
<h1><?php echo $client['Client']['lastname']; ?> <?php echo $client['Client']['firstname']; ?> (<?php echo $this->Html->link('Edit', array('action' => 'edit', $client['Client']['id'])); ?>)</h1>
<p><strong>Date of Birth:</strong> <?php echo $client['Client']['dateofbirth']; ?></p>
<h3>Appointments</h3>
<table>
<?php foreach ($appointments as $appointment): ?>
<tr>
<td>
<?php echo $this->Html->link($appointment['Appointment']['date'],
array('admin' => true, 'controller' => 'appointment', 'action' => 'view', $appointment['Appointment']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
You need to setup
hasManyassociation betweenClient->Appointmentmodels (see docs, exactly asUser->Commentassociation is), and reversebelongsToassociation betweenAppointment->Client(see same doc).If your DB tables and keys are named according to Cake conventions and you’re using PHP 5, this is all that you need:
Recursion is set to 1 (as you need it) by default, so if you haven’t changed it anywhere – your code should work.