Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
第二个模型的cakePHP分页器问题_Cakephp_Paginate_Paginator - Fatal编程技术网

第二个模型的cakePHP分页器问题

第二个模型的cakePHP分页器问题,cakephp,paginate,paginator,Cakephp,Paginate,Paginator,我还是个新手,但我正在与paginator进行斗争,将我的视图限制为10条记录[properties],并在properties模型中的一个字段上对其进行排序。为了让分页器控制我的视图,我在代码中犯了什么错误或遗漏了什么 我有两个模型,区域和属性,一个区域有许多属性,例如,我的视图是/region/view/13这显示了区域13的所有属性 paginator显示的属性数量正确,页面设置正确,所有内容看起来都正确,但paginator并没有限制我的查看,它只是在一个巨大的列表中显示该区域的所有属性

我还是个新手,但我正在与paginator进行斗争,将我的视图限制为10条记录[properties],并在properties模型中的一个字段上对其进行排序。为了让分页器控制我的视图,我在代码中犯了什么错误或遗漏了什么

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

paginator显示的属性数量正确,页面设置正确,所有内容看起来都正确,但paginator并没有限制我的查看,它只是在一个巨大的列表中显示该区域的所有属性,而不是将每页的查看限制为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' => ''
    )
);

}
视图:




总属性:
排序方式: 睡觉
::属性参考号()



您正在将分页结果设置为
$regions
,但在您的视图中,您正在循环并显示
$region
(单数),这是上面的一个直接向上的
查找()
,带有递归2,它将提取所有属性


因此-尽管还有许多其他事情需要用你的代码来清理,但现在,在你看来,只需使用
$region
(复数)而不是
$region
单数。

Dave,你的建议会带来错误,尽管我是一个新手,但我认为我遵守了MVC命名的建议。视图是存储在文件夹/View/Regions/View.ctp中的多个视图,模型为singularise。需要将数据推送到区域/视图,因此不要认为这样做是错误的。我需要将递归设置为2,以便能够检索每个属性的图像,因为我在该区域中。[Region Property PropertyImage]您是否介意解释一下清理我的代码需要做的许多其他事情?我渴望学习。非常感谢。我从来没有提到过更改视图或模式的名称。您使用的变量不正确。保存分页结果的变量(在视图中)是
$regions
。我不在乎你是否叫它
$pizza
,但这不是现在设定的。然后,您试图使用
$region
单数来显示分页结果,这不是保存分页结果的变量,而是保存整个结果集的变量,这就是您的问题。谢谢,我知道您的意思,我将尝试更改此项。似乎我很难让这两个变量一起工作,有人能给我解释一下,告诉我该怎么做才能让它发挥作用吗?我已经有了。如果您有其他具体问题,请随意为每个问题创建一个问题。
    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>