蛋糕 PHP 1.2 分页链接被禁用


CakePHP 1.2 pagination link is disabled

在我的CakePHP应用程序中,我的自定义分页根本不起作用。我面临三个问题:

  1. 它始终只显示一个页面
  2. 链接已禁用
  3. 结束记录不正确。我总共有 7 条记录。我使用了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 年非常古老的项目。

当他打电话时:

$this->set('HomeLoanDistributions', $this->paginate($conditions));

我认为他使用了:http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

并且$limit应该在分页组件的配置中传递..不是吗?

AppController 或这个中有$paginate变量吗?


编辑 : 尝试使用来自您的 MOdel 的分页:

改变

$this->set('HomeLoanDistributions', $this->paginate($conditions));

$this->set('HomeLoanDistributions', $this->HomeLoanDistribution->paginate($conditions) );