0
votes

Novice question about the Paginator. I have a DB with tables; country, regions and properties [and a few more tables] Now when I do a view for the regions to displays the properties sorted by region: for example domain.com/regions/view/33 the Paginator counts and displays information related to the regions [a total of 60 regions] and I would like to have the Paginator to show/count for the amount of properties [3 properties] in region 33, How to get this done?

Here is my code in the 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());
}

This is the code to display the Paginator in my 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>


This is the code in the Region.php Model.

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' => ''
    )
);

Any help much appreciated.

1

1 Answers

0
votes

Use CakePHP's counterCache to retain a constant count of the number of properties per region. It's crazy-easy, and CakePHP does all the work of keeping track for you.

Then, you'll just have a "property_count" field in your regions table, and voila - you can sort by it, filter by it...etc etc etc.

Every time you add a property or delete a property, CakePHP will update the count for you automatically.