开发者

cakephp getting data from different model

I'm new to cakephp and I've a problem with building my application. In my application I have the models called Customer , Job, Jobtask, Jobtasksvehicle and Vehicle. Their database relationship is Customer->Job->Jobtask->Jobtasksvehicle->Vehicle. I have a page called job sheet, in that sheet I've to display customer name, job id, job task, and vehicle name. I can get customer name, job id and job task display but I cannot figure out how to display vehicle name in there. Can anyone help me.

This is the model for the page

    function sch($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid job', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->set('job', $this->Job->read(null, $id));

I want to display vehicle name in this table

<div class="related">

<?php
    $i = 0;
    foreach ($job['Jobtask'] as $jobtask):
        $class = null;
        if ($i++ % 2 == 0) {
            $class = ' class="altrow"';
        }
    ?>
    <tr<?php echo $class;?>>
        <td><?php echo $jobtask['id'];?></td>
        <td><?php echo $jobtask['job_id'];?></td>
        <td><?php echo $jobtask['rate_id'];?></td>
        <td><?php echo $jobtask['type'];?></td>
        <td><?php echo $jobtask['date'];?></td>
        <td><?php echo $jobtask['starttime'];?></td>
        <td><?php echo $jobtask['timeband'];?></td>
        <td><?php echo $jobtask['settlement'];?></td>       
        <td><?php echo $job['Jobtasksvehicle']['vehicle_id'];?></td>    

        <td class="actions">



            <?php echo $this->Html->link(__('Allocate Vehicle', true), array('controller' => 'jobtasksvehicles','action' => 'add', $jobtask['id'])); ?>
            <?php echo $this->Html->link(__('View', true), array('controller' => 'jobtasks', 'action' => 'view', $jobtask['id'])); ?>
            <?php echo $this->Html->link(__('Edit', true), array('controller' => 'jobtasks', 'action' => 'edit', $jobtask['id'])); ?>
            <?php echo $this->Html->link(__('Delete', true), array('controller' => 'jobtasks', 'action' => 'delete', $jobtask['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $jobtask['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
</table>

This is the relationship betweens models

Customer hasMany Job

Job belongsTo Customer

Job hasMany Jobtask

Jobtask belongsTo Job

Jobtask hasMa开发者_开发知识库ny Jobtasksvehicle

Jobtasksvehicle belongsTo Jobtask and Vehicle

Vehicle hasMany Jobtasksvehicle


Using the Containable behaviour as Ross mentions is the best approach. Add to your Job model:

var $actsAs = array('Containable');

And in your controller:

$job = $this->Job->find('first', array(
    'conditions' => array('Job.id' => $id),
    'contain' => array(
        'Jobtask' => array(
            'Jobtasksvehicle' => array(
                'Vehicle'
             ),
         ),
    ),
));
$this->set(compact('job'));

See the Containable documentation for more help. I'll leave the wrangling of data in the view up to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜