0

仍然是新手,但我正在与分页器进行斗争,以将我的视图限制为 10 条记录 [属性],并在属性模型中的字段上对其进行排序。我在代码中做错了什么或错过了什么让分页器控制我的视图???

我有 2 个模型,区域和属性,一个区域有很多属性,例如,我的视图是 /region/view/13 这显示了区域 13 的所有属性。

分页器正在显示正确数量的属性,正确设置页面并且一切看起来都正确,但是分页器并没有限制我的视图,它只是在一个巨大的列表中显示该区域的所有属性,而不是将视图限制为每页 10 个。尽管浏览器中的 url 似乎没问题,但每次睡眠的排序也不起作用。

/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>
4

1 回答 1

1

您正在将分页结果设置为$regions,但在您看来,您正在循环并显示$region(单数),这是上面的直线find(),带有递归 2,它将提取所有属性。

所以 - 尽管你的代码还有很多其他的东西需要清理,但现在,只需在你的视图中使用$regions(复数)而不是单数。$region

于 2013-05-03T00:28:55.307 回答