paginate - cakePHP Paginator issue with second model -
still novice, fighting paginator limit view 10 records [properties] , sort on field in properties model. doing wrong or miss in code paginator control view???
i have 2 models, regions , properties, region has many properties, example, view /region/view/13 shows properties region 13.
the paginator displaying correct amount of properties, set pages correct , seems correct, paginator not limiting view, displays properties region in 1 huge list instead of limit view 10 per page. sort per sleeps doesn't work either although url in browser seems ok.
/regions/view/13/sort:sleeps/direction:asc /regions/view/13/sort:sleeps/direction:desc
model:
<?php app::uses('appmodel', 'model'); /** * region model * * @property country $country * @property property $property */ 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' => '' ) ); }
controller:
public function view($id = null) { if (!$this->region->exists($id)) { throw new notfoundexception(__('invalid region')); } $this->region->recursive = 2; // related associated model data, region -> properties -> propertyimages $options = array('conditions' => array('region.' . $this->region->primarykey => $id)); $total = $this->region->property->find('count', array( 'conditions' => array('property.region_id' => $id) )); $this->set('total', $total); // push counter view. $this->set('region', $this->region->find('first', $options)); // push properties view. $this->paginate = array( 'limit' => 10, 'conditions' => array('property.region_id' => $id), ); $this->region->property->virtualfields['sleeps'] = 'property.sleeps'; $this->set('regions', $this->paginate('property')); }
view:
<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> <br> <?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}'))); ?><br> total properties: <?php echo $total . "\n"; ?><br> sort by: <?php echo $this->paginator->sort('sleeps'); ?> <?php if (!empty($region['property'])): ?> <div class="regionviewfooter"></div> <?php $i = 0; foreach ($region['property'] $property): ?> <!-- need 1 image show --> <?php foreach ($property['propertyimage'] $key =>$propertyimage): ?> <div class="regionview"> <div class="regionviewleft"> <?php echo '<span style="font-size:12px;line-height:18px;font-weight:bold;">'; echo $this->html->link(__($property['description']), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?> <div class="regionviewheader">sleeps <?php echo $property['sleeps']; echo ' :: ' ?> <?php if ($property['minprice'] == 0) { echo 'please call prices'; } else { echo 'prices '; echo $this->number->currency($property['minprice'], 'gbp'); if ($property['maxprice'] == 0) { echo ' pw'; } else { echo ' - '; echo $this->number->currency($property['maxprice'], 'gbp'); echo ' pw'; } } echo '<span style="font-size:11px;line-height:18px;font-weight:normal;">'; echo ' :: '; echo $this->html->link(__('view property'), array('controller' => 'properties', 'action' => 'view', $property['id'])); echo '</span>'; ?> </div> <?php echo ($property['shortdesc']); ?><br> <a href="../../enqlist">add enquiry list</a> :: property ref. (<?php echo strtoupper($property['ref']); ?>) </div> <div class="regionviewright"> <!-- display image --> <?php echo $this->html->image(($propertyimage['filename']), array('alt' => ($propertyimage['description']),'width' => '150' ,'height' => '77')); $key ?> <?php $key++; ?> </div> </div> <div class="clear"></div> <div class="regionviewfooter"></div> <?php if ($key == 1) { break; // stop, did 1 image. } ?> <?php endforeach; ?> <?php endforeach; ?> <?php endif; ?> <br><a href="#top">top of page</a><br><br>
you're setting paginated results $regions
, yet in view, you're looping through , displaying $region
(singular), straight-up find()
above, recursive 2, pull properties.
so - although there many other things need cleaned code, now, use $regions
(plural) instead of $region
singular in view.
Comments
Post a Comment