CakePHP - 未定义属性:View :: $ Paginator

时间:2011-06-15 06:52:31

标签: php cakephp undefined

我有以下控制器,我收到错误

Notice (8): Undefined property: View::$Paginator [CORE/plugins/forum/views/galleries/index.ctp, line 5]

控制器:

<?php
class GalleriesController extends AppController {

    var $name = 'Galleries';

    function index() {
        $this->Gallery->recursive = 0;
        $this->set('galleries', $this->paginate());
    }
}
?>

查看:

<div class="galleries index">
    <h2><?php __('Galleries');?></h2>
    <table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('id');?></th>
            <th><?php echo $this->Paginator->sort('name');?></th>
            <th class="actions"><?php __('Actions');?></th>
    </tr>
    <?php
    $i = 0;
    foreach ($galleries as $gallery):
        $class = null;
        if ($i++ % 2 == 0) {
            $class = ' class="altrow"';
        }
    ?>
    <tr<?php echo $class;?>>
        <td><?php echo $gallery['Gallery']['id']; ?>&nbsp;</td>
        <td><?php echo $gallery['Gallery']['name']; ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View', true), array('action' => 'view', $gallery['Gallery']['id'])); ?>
            <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $gallery['Gallery']['id'])); ?>
            <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $gallery['Gallery']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $gallery['Gallery']['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </table>
    <p>
    <?php
    echo $this->Paginator->counter(array(
    'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
    ));
    ?>  </p>

    <div class="paging">
        <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
     |  <?php echo $this->Paginator->numbers();?>
 |
        <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
    </div>
</div>
<div class="actions">
    <h3><?php __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New Gallery', true), array('action' => 'add')); ?></li>
        <li><?php echo $this->Html->link(__('List Images', true), array('controller' => 'images', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Image', true), array('controller' => 'images', 'action' => 'add')); ?> </li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:4)

看起来PaginatorHelper没有加载。通常,当您调用$this->paginate()时,CakePHP会自动加载帮助程序。您可以通过在var $helpers = array('Paginator');正下方添加var $name = 'Galleries';来明确加载它,但其他错误。

Controller类中的paginate方法在返回到添加PaginatorHelper的文件底部之前只返回一次,如果找不到模型则返回。它应该触发一个错误,上面写着“Controller :: paginate() - 无法在控制器GalleriesController中找到模型库”。你可能有错误被抑制,这可以解释为什么你没有看到问题。

在执行任何启用错误的调用之前,请尝试在操作中添加ini_set('display_errors', true);display_errors(E_ALL);Configure::write('debug', 2);

答案 1 :(得分:0)

我把这段代码工作得很好..

    echo $paginator->first('First');
        echo "  ";
        if($paginator->hasPrev()){
            echo $paginator->prev('<<');
            echo "  ";`enter code here`
        }
    echo $paginator->numbers(array('modulus' => 2)); 
    echo "  ";
    if($paginator->hasNext()){ 
        echo $paginator->next('>>');
        echo "  ";
    }
    echo $paginator->last('Last');
    echo "  ";