2013-05-02 306 views
0

仍然是一個新手,但我與分頁器​​爭取將我的視圖限制爲10個記錄[屬性]並將其排序在屬性模型的字段中。我在做什麼錯或錯過我的代碼讓paginator來控制我的觀點?cakePHP與第二個模型的分頁問題

我有2個模式,地區和屬性,一個區域具有許多性質,例如,我的觀點是/區域/視圖/ 13這示出了用於區域的所有屬性13.

該分頁程序時顯示正確的量的屬性,設置頁面正確,所有似乎都是正確的,但paginator不限制我的看法,它只顯示該區域的所有屬性在一個巨大的列表中,而不是將視圖限制爲每頁10個。 雖然瀏覽器中的網址似乎沒問題,但每次睡眠的排序都不起作用。

/regions/view/13/sort:sleeps/direction:asc 
/regions/view/13/sort:sleeps/direction:desc 

型號:

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

} 

控制器:

public function view($id = null) { 
    if (!$this->Region->exists($id)) { 
     throw new NotFoundException(__('Invalid region')); 
    } 
      $this->Region->recursive = 2; // related to 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 to counter the view. 
      $this->set('region', $this->Region->find('first', $options)); // Push the properties to the 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')); 
}  

查看:

<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'] as $property): ?> 
<!-- We only need 1 image to show up --> 
<?php foreach ($property['PropertyImage'] as $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&nbsp;<?php echo $property['sleeps']; echo ' :: ' ?> 
        <?php 
        if ($property['minPrice'] == 0) { 
         echo 'Please call for prices'; 
        } else { 
         echo 'Prices from '; 
         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 to my 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, we did 1 image. 
     } 
    ?> 
    <?php endforeach; ?> 
    <?php endforeach; ?> 
<?php endif; ?> 
<br><a href="#top">Top of Page</a><br><br> 

回答

1

你設置你的分頁結果$regions,但在你看來,你正在循環和顯示$region(單數),這是一個直線上面的find(),遞歸2,它將拉動所有屬性。

所以 - 雖然還有許多其他事情需要用您的代碼清理,但現在,只需在您的視圖中使用$regions(複數)而不是$region單數。

+0

戴夫,你的建議引發錯誤,雖然是新手,但我想我一直遵循MVC命名的建議。視圖是複數存儲在文件夾/View/Regions/view.ctp模型singularise。數據需要推送到地區/視圖,所以不要認爲這是錯誤的。我需要將遞歸設置爲2,以便能夠檢索每個屬性的圖像,因爲我在該區域。 [Region-Property-PropertyImage]請介紹一下清理我的代碼需要做的許多其他事情嗎?我渴望學習。非常感謝。 – Nick 2013-05-03 09:40:48

+0

我從來沒有提到改變視圖或模式的名稱。您使用的變量不正確。保存分頁結果的變量(在視圖中)是「$ regions」。我不在乎你是否稱它爲'$披薩',但這不是它現在設定的。然後,您嘗試使用'$ region'單數來顯示分頁結果,這不是提供分頁結果的變量 - 它是保存整個結果集的變量,因此是您的問題。 – Dave 2013-05-03 13:36:34

+0

謝謝,我知道你的意思,我會盡量讓這個改變。 – Nick 2013-05-03 20:23:32