开发者

CakePHP Pull data from two tables

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 hasMany association between Client->Appointment models (see docs, exactly as User->Comment association is), and reverse belongsTo association between Appointment->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:

class Client extends AppModel
{
    var $hasMany = 'Appointment';
}

class Appointment extends AppModel
{
    var $belongsTo = 'Client';
}

Recursion is set to 1 (as you need it) by default, so if you haven't changed it anywhere - your code should work.


you should name the foreign key 'client_id' instead. That way when you bake it, Cake can automatically make the connection. Also, for your current purpose, recursive=1 would be fine. But I'd suggest to look into Containable. It is a very useful tool.


Make sure in your models both models have the association vars set. i.e. in Client model there is a hasMany association set with foreign key client_id and in Appointment model there is belongsTo with foreign key client_id. If this is alright check if in your controllers you are calling find with recursive set to greater than -1.

    $this->Client->find('all', 'recursive' => 1);

Now in your clients model you should have a $hasMany field like following

    var $hasMany = array(
    'Appointment' => array(
        'className' => 'Appointment',
        'foreignKey' => 'client_id',
        'dependent' => false,
        'conditions' => ''
    )

And in controller's action where you need to pull all related data, for example in your admin_view action use

    $this->set('client', $this->Client->find('first', 'recursive' => 1));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜