0

关于 Paginator 的新手问题。我有一个带表格的数据库;国家、地区和属性 [以及更多表格] 现在,当我查看区域以显示按区域排序的属性时:例如 domain.com/regions/view/33 分页器计数并显示与区域相关的信息[总共 60 个区域],我想让 Paginator 显示/计算区域 33 中的属性 [3 个属性] 的数量,如何完成?

这是我在 RegionsController.php 中的代码

public function index() {
    $this->Region->recursive = 0;
    $this->paginate = array('limit' => 10,'order' => 'Region.id ASC');
    $this->set('regions', $this->paginate());
}

public function view($id = null) {
    if (!$this->Region->exists($id)) {
        throw new NotFoundException(__('Invalid region'));
    }
    $options = array('conditions' => array('Region.' .$this->Region->primaryKey => $id));
    $this->set('region', $this->Region->find('first', $options));
    $this->paginate = array('limit' => 10,'order' => 'Region.id ASC');
    $this->set('regions', $this->paginate());
}

这是在我的 view.ctp 中显示分页器的代码

<br />
<div class="regions view">
<b><center><?php echo nl2br(h($region['Region']['titletag'])); ?></center></b>
<div class="paging">
<?php
    echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
    echo $this->Paginator->numbers(array('separator' => ''));
    echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>


这是 Region.php 模型中的代码。

class Region extends AppModel {

/**
 * Display field
*
* @var string
*/
public $displayField = 'regionname';


/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
    'Country' => array(
        'className' => 'Country',
        'foreignKey' => 'country_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
    'Property' => array(
        'className' => 'Property',
        'foreignKey' => 'region_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

非常感谢任何帮助。

4

1 回答 1

0

使用CakePHP 的 counterCache来保持每个区域的属性数量的恒定计数。这非常简单,CakePHP 会为您完成所有跟踪工作。

然后,您的regions表中将只有一个“property_count”字段,瞧 - 您可以按它排序,按它过滤......等等等等。

每次添加或删除属性时,CakePHP 都会自动为您更新计数。

于 2013-04-10T14:00:29.607 回答