在我的CakePHP应用程序中,我的自定义分页根本不起作用。我面临三个问题:
LIMIT 3
,因此每页只能看到3条记录,最后一条记录是记录号。第一页3。但相反,记录否。在第一页中显示7作为结束记录。我已将debug mode
设置为2
,以尝试找出我的代码中可能存在的任何问题。但无法找到任何。
这是我的控制器代码:
var $name = 'HomeLoanDistributions';
function index() {
$user = $this->Session->read('User');
$user_role = $user['User']['user_role_id'];
$actions = $this->Session->read('actions');
$display_actions = array();
foreach ($actions as $action) {
array_push($display_actions, $action['pm_controller_actions']['name']);
}
$this->set('display_actions', $display_actions);
$this->set('user_role', $user_role);
$branch_id = $this->branchCheck();
$this->set('branch_id', $branch_id);
$conditions = array('branch_id' => $branch_id);
$this->set('HomeLoanDistributions', $this->paginate($conditions));
$this->HomeLoanDistribution->Branch->recursive = 0;
$this->set('BranchInformation', $this->HomeLoanDistribution->Branch->read(array('Branch.id', 'Branch.name', 'Region.name', 'District.name', 'SubDistrict.name', 'Cluster.name'), $branch_id));
}
function branchCheck() {
// check for matching branch
if (isset($this->params['named']['branch_id'])) {
if (empty($this->params['named']['branch_id'])) {
$this->Session->setFlash(__('Branch could not be found. Please, try again.', true));
$this->redirect(array('controller' => 'Branches', 'action' => 'index', $this->name));
} else {
$this->HomeLoanDistribution->Branch->recursive = -1;
if ($this->HomeLoanDistribution->Branch->find('count', array('conditions' => array('Village.id' => $this->params['named']['branch_id']))) != 1) {
$this->Session->setFlash(__('The Branch could not be found. Please, try again.', true));
$this->redirect(array('controller' => 'Branches', 'action' => 'index', $this->name));
}
}
return $this->params['named']['branch_id'];
}
}
这是我的型号代码:
var $name = 'HomeLoanDistribution';
var $actsAs = array('Logable' => array(
'userModel' => 'User',
'userKey' => 'user_id',
'change' => 'list', // options are 'list' or 'full'
'description_ids' => TRUE // options are TRUE or FALSE
));
var $validate = array(
'entry_date' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
),
'branch_id' => array('numeric'),
'customer_id' => array('numeric'),
'home_group_id' => array('numeric'),
'voucher_no' => array('numeric'),
'no_of_installment' => array('numeric'),
'loan_amount' => array('numeric'),
'service_charge' => array('numeric'),
'security' => array('numeric'),
'loan_taken_term' => array('numeric'),
'installment_amount' => array('numeric'),
'installment_service_charge' => array('numeric'),
'effective_date' => array('numeric'),
);
var $belongsTo = array(
'Branch' => array(
'className' => 'Branch',
'foreignKey' => 'branch_id',
'conditions' => '',
'fields' => 'id,name',
'order' => ''
),
);
function paginate($conditions, $fields, $order, $page = 1, $recursive = null, $extra = array()) {
$recursive = 0;
$fields = array('entry_date');
$group = array('branch_id');
$order = array('entry_date DESC');
$limit = 3;
return $this->find('all', compact('fields', 'order', 'limit', 'page', 'recursive', 'group'));
$this->paginateCount($conditions);
}
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$recursive = 0;
$fields = array('entry_date');
$group = array('branch_id');
$order = array('entry_date DESC');
$results = $this->find('all', compact('fields', 'order', 'limit', 'page', 'recursive', 'group'));
return count($results);
}
我的观看代码:
<?php echo $this->renderElement('BranchInformation'); ?>
<?php
$paginator->options(
array(
'url' => array('controller' => 'HomeLoanDistributions', 'action' => $this->action, 'branch_id' => $BranchInformation['Branch']['id'])));
?>
<h2><?php __('Home Loan Distribution'); ?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo 'SL NO.'; ?></th>
<th><?php echo $paginator->sort('Loan Distribution Month', 'entry_date'); ?></th>
<th class="actions"><?php __('Actions'); ?></th>
<!--th class="actions"><--?php __('Actions');?></th-->
</tr>
<?php
$start = (int) $paginator->counter('%start%');
$i = 0;
foreach ($HomeLoanDistributions as $HomeLoanDistribution):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class; ?>>
<td>
<?php echo $start++; ?>
</td>
<td class="taRight">
<?php
$returnDate = date_format(date_create($HomeLoanDistribution['HomeLoanDistribution']['entry_date']), "M-Y");
print_r($returnDate);
?>
</td>
<td class="actions">
<?php echo $html->link(__('View', true), array('action' => 'view', $HomeLoanDistribution['HomeLoanDistribution']['entry_date'], 'branch_id' => $BranchInformation['Branch']['id'])); ?>
<?php echo $html->link(__('Edit', true), array('action' => 'edit', $HomeLoanDistribution['HomeLoanDistribution']['entry_date'], 'branch_id' => $BranchInformation['Branch']['id'])); ?>
<?php echo $html->link(__('Delete', true), array('action' => 'delete', $HomeLoanDistribution['HomeLoanDistribution']['entry_date'], 'branch_id' => $BranchInformation['Branch']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $HomeLoanDistribution['HomeLoanDistribution']['entry_date'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="paging">
<?php echo $paginator->prev('<< ' . __('previous', true), array(), null, array('class' => 'disabled')); ?>
<?php echo $paginator->numbers(); ?>
<?php echo $paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled')); ?>
<span style="float:right;"><?php
echo $paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
));
?></span>
</div>
我的CakePHP版本是1.2.5,我的PHP版本是5.2.11,因为这是一个非常古老的项目,从2008-09开始。
答案 0 :(得分:0)
当他打电话时:
$this->set('HomeLoanDistributions', $this->paginate($conditions));
我认为他使用过:http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
和$ limit应该在PaginationComponent的配置中传递..不是吗?
在AppController中还是在这个中有$ paginate var?
编辑:尝试使用MOdel中的paginate:
更改
$this->set('HomeLoanDistributions', $this->paginate($conditions));
通过
$this->set('HomeLoanDistributions', $this->HomeLoanDistribution->paginate($conditions) );