开发者

CakePHP: Related values in index()'s paginated list

I'm attempting to edit an index() function to display objects that the index()'s model has a "hasMany" relationship with.

If that's a mouth-full, in other words, I'm attempting to take the "Related Values" that can appear in the baked view() "view" to appear in the index() "view" in the table rows immediately following the main objects.

I'm sorry if I lack the vernacular to explain this better but maybe my example code will help.

I attempted to take the snippet from the view.ctp and insert it within the index.ctp's foreach loop but, that did not work. Clearly I am missing the majority of what's necessary.

My main object is "Projects" which "hasMany" "Statuses." I'm not doing anything in the controller or model to achieve this and thought it'd be just view editing.

index.ctp:

    <?php
$i = 0;
foreach ($projects as $project):
    $class = null;
    if ($i++ % 2 == 0) {
        $class = ' class="altrow"';
    }
?>
<tr<?php echo $class;?>>
    <td><?php echo $project['Project']['id']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['name']; ?>&nbsp;</td>
    <td>
        <?php echo $this->Html->link($project['Brand']['name'], array('controller' => 'brands', 'action' => 'view', $project['Brand']['id'])); ?>
    </td>
    <td>
        <?php echo $this->Html->link($project['Jobtype']['name'], array('controller' => 'jobtypes', 'action' => 'view', $project['Jobtype']['id'])); ?>
    </td>
    <td><?php echo $project['Project']['job_number']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['created']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['hours_to_date']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['number_of_concepts']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['number_of_resizes']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['requires_ia']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['requires_tech']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['requires_seo']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['complexity_level']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['vendor_name']; ?>&nbsp;</td>
    <td><?php echo $project['Project']['team_members']; ?>&nbsp;</td>
    <td class="actions">
        <?php echo $this->Html->link(__('View', 开发者_如何学运维true), array('action' => 'view', $project['Project']['id'])); ?>
        <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $project['Project']['id'])); ?>
        <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $project['Project']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $project['Project']['id'])); ?>
    </td>
</tr>
<tr>
    <td colspan="16">
        <div class="related">
            <?php if (!empty($project['Status'])):?>
            <table cellpadding = "0" cellspacing = "0">
            <tr>
                <th><?php __('Id'); ?></th>
                <th><?php __('User Id'); ?></th>
                <th><?php __('Project Id'); ?></th>
                <th><?php __('Created'); ?></th>
                <th><?php __('Percentage Complete'); ?></th>
                <th><?php __('Status'); ?></th>
                <th class="actions"><?php __('Actions');?></th>
            </tr>
            <?php
                $i = 0;
                foreach ($project['Status'] as $status):
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }
                ?>
                <tr>
                                         <?php echo $class;?>>
                    <td><?php echo $status['id'];?></td>
                    <td><?php echo $status['user_id'];?></td>
                    <td><?php echo $status['project_id'];?></td>
                    <td><?php echo $status['created'];?></td>
                    <td><?php echo $status['percentage_complete'];?></td>
                    <td><?php echo $status['status'];?></td>
                    <td class="actions">
                        <?php echo $this->Html->link(__('View', true), array('controller' => 'statuses', 'action' => 'view', $status['id'])); ?>
                        <?php echo $this->Html->link(__('Edit', true), array('controller' => 'statuses', 'action' => 'edit', $status['id'])); ?>
                        <?php echo $this->Html->link(__('Delete', true), array('controller' => 'statuses', 'action' => 'delete', $status['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $status['id'])); ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </table>
        <?php endif; ?>
        </div>
    </td>
</tr>

Thanks in advanced. Any responses or hints welcome.


if you need related data of each record

you should set

$this->recursive = 1 // or 2;

and call regular paginate method; every record will now contain it's related data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜