【发布时间】:2023-04-10 22:00:01
【问题描述】:
我正在使用 Cakephp 3,我正在尝试为相关数据创建分页,换句话说,所有记录都属于第一个表。
示例
我有两张桌子
用户
User_Profiles
用户有很多个人资料,所以当我点击任何用户时,它会在查看功能中显示相关的个人资料,
想象一下,如果我有 10000 个特定用户的个人资料,那么很难显示所有 1000 个个人资料,我如何为他们分页?
这是我的控制器
public function view($id = null)
{
$distributor = $this->Distributors->get($id, [
'contain' => ['DistributorUsers', 'Participants', 'Payments']
]);
$this->set('distributor', $distributor);
$this->set('_serialize', ['distributor']);
}
从该函数我们可以看到我们有 3 个表是相关的,如果我在每个表上都有 10000 条记录,它将在视图模板上可用而无需分页,这真的很难加载页面甚至阅读所有内容,
这是我的看法
<table class="table table-condensed table-hover">
<thead>
<tr>
<th colspan="3"> <?= h($distributor->name) ?> </th>
</tr>
</thead>
<tbody>
<tr>
<td><?= __('Name') ?></th>
<td><?= h($distributor->name) ?></td>
</tr>
<tr>
<td><?= __('Location') ?></th>
<td><?= h($distributor->location) ?></td>
</tr>
<tr>
<td><?= __('Phone') ?></th>
<td><?= h($distributor->phone) ?></td>
</tr>
<tr>
<td><?= __('Id') ?></th>
<td><?= $this->Number->format($distributor->id) ?></td>
</tr>
<tr>
<td><?= __('Created') ?></th>
<td><?= h($distributor->created) ?></td>
</tr>
<tr>
<td><?= __('Active') ?></th>
<td><?= $distributor->active ? __('Yes') : __('No'); ?></td>
</tr>
</table>
<div class="related">
<h4><?= __('Related Distributor Users') ?></h4>
<?php if (!empty($distributor->distributor_users)): ?>
<table class="table table-hover" id="sample-table-1">
<thead>
<tr>
<th><?= __('Id') ?></th>
<th><?= __('Level') ?></th>
<th><?= __('Username') ?></th>
<th><?= __('Email') ?></th>
<th><?= __('Created') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($distributor->distributor_users as $distributorUsers): ?>
<tr>
<td><?= h($distributorUsers->id) ?></td>
<td><?= h($distributorUsers->level) ?></td>
<td><?= h($distributorUsers->username) ?></td>
<td><?= h($distributorUsers->email) ?></td>
<td><?= h($distributorUsers->created) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['controller' => 'DistributorUsers', 'action' => 'view', $distributorUsers->id]) ?>
<?= $this->Html->link(__('Edit'), ['controller' => 'DistributorUsers', 'action' => 'edit', $distributorUsers->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['controller' => 'DistributorUsers', 'action' => 'delete', $distributorUsers->id], ['confirm' => __('Are you sure you want to delete # {0}?', $distributorUsers->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<div class="related">
<h4><?= __('Related Participants') ?></h4>
<?php if (!empty($distributor->participants)): ?>
<table class="table table-hover" id="sample-table-1">
<thead>
<tr>
<th><?= __('Id') ?></th>
<th><?= __('Age') ?></th>
<th><?= __('Dateofbirth') ?></th>
<th><?= __('Gender') ?></th>
<th><?= __('Address') ?></th>
<th><?= __('Mobile') ?></th>
<th><?= __('Relatives Phone') ?></th>
<th><?= __('Has Disease') ?></th>
<th><?= __('Disease') ?></th>
<th><?= __('Ip Address') ?></th>
<th><?= __('Distributor Id') ?></th>
<th><?= __('Event Id') ?></th>
<th><?= __('Is First Time') ?></th>
<th><?= __('Is Payment Done') ?></th>
<th><?= __('Confirm Attendance') ?></th>
<th><?= __('First Name') ?></th>
<th><?= __('Second Name') ?></th>
<th><?= __('Third Name') ?></th>
<th><?= __('Last Name') ?></th>
<th><?= __('Lockbox Number') ?></th>
<th><?= __('Created') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($distributor->participants as $participants): ?>
<tr>
<td><?= h($participants->id) ?></td>
<td><?= h($participants->age) ?></td>
<td><?= h($participants->dateofbirth) ?></td>
<td><?= h($participants->gender) ?></td>
<td><?= h($participants->address) ?></td>
<td><?= h($participants->mobile) ?></td>
<td><?= h($participants->relatives_phone) ?></td>
<td><?= h($participants->has_disease) ?></td>
<td><?= h($participants->disease) ?></td>
<td><?= h($participants->ip_address) ?></td>
<td><?= h($participants->distributor_id) ?></td>
<td><?= h($participants->event_id) ?></td>
<td><?= h($participants->is_first_time) ?></td>
<td><?= h($participants->is_payment_done) ?></td>
<td><?= h($participants->confirm_attendance) ?></td>
<td><?= h($participants->first_name) ?></td>
<td><?= h($participants->second_name) ?></td>
<td><?= h($participants->third_name) ?></td>
<td><?= h($participants->last_name) ?></td>
<td><?= h($participants->lockbox_number) ?></td>
<td><?= h($participants->created) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['controller' => 'Participants', 'action' => 'view', $participants->id]) ?>
<?= $this->Html->link(__('Edit'), ['controller' => 'Participants', 'action' => 'edit', $participants->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['controller' => 'Participants', 'action' => 'delete', $participants->id], ['confirm' => __('Are you sure you want to delete # {0}?', $participants->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<div class="related">
<h4><?= __('Related Payments') ?></h4>
<?php if (!empty($distributor->payments)): ?>
<table class="table table-hover" id="sample-table-1">
<thead>
<tr>
<th><?= __('Id') ?></th>
<th><?= __('Participant Id') ?></th>
<th><?= __('Distributor Id') ?></th>
<th><?= __('Event Id') ?></th>
<th><?= __('Distributor User Id') ?></th>
<th><?= __('Amount') ?></th>
<th><?= __('Created') ?></th>
<th><?= __('Ip Address') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($distributor->payments as $payments): ?>
<tr>
<td><?= h($payments->id) ?></td>
<td><?= h($payments->participant_id) ?></td>
<td><?= h($payments->distributor_id) ?></td>
<td><?= h($payments->event_id) ?></td>
<td><?= h($payments->distributor_user_id) ?></td>
<td><?= h($payments->amount) ?></td>
<td><?= h($payments->created) ?></td>
<td><?= h($payments->ip_address) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['controller' => 'Payments', 'action' => 'view', $payments->id]) ?>
<?= $this->Html->link(__('Edit'), ['controller' => 'Payments', 'action' => 'edit', $payments->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['controller' => 'Payments', 'action' => 'delete', $payments->id], ['confirm' => __('Are you sure you want to delete # {0}?', $payments->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div></div>
请帮忙,谢谢
【问题讨论】:
-
i have two tables您的代码使用完全不同的名称 - 请描述您的实际问题,而不是简化版本 - 即使描述与问题中的代码一致。
标签: cakephp cakephp-3.0