cakephp间接修改重载属性PaginatorHelper

时间:2012-02-09 18:05:48

标签: php cakephp pagination cakephp-2.0 paginator

我有以下搜索表单

<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));?>
<?php echo $this->Form->input('SearchTerm',array('label' => 'Find:')); ?>
<?php $options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'); ?>
<?php echo $this->Form->input('options', array('type'=>'select', 'label'=>'Search:', 'options'=>$options)); ?>

<?php echo $this->Form->end('search'); ?>  

<?php if($rs!=0){?>
<?php if($rs!=null) { ?>
    results found : <?php print $results; //print_r ($result['Order']['full_name']); ?>
<h1 class="ico_mug">Results Matching Term: <?php print '"'.$term.'"' ;?></h1>
<table id="table">
    <tr>

        <th>Client Name</th>
        <th>Order Code</th>
        <th>Phone</th>
        <th>Received Order</th>
        <th>Working On Order </th>
        <th>Order Ready </th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($rs as $order): ?>
    <tr>

        <td>
            <?php echo $this->Html->link($order['Order']['full_name'],
            array('controller' => 'orders', 'action' => 'view', $order['Order']['id'])); ?>
        </td>
        <td><?php echo $order['Order']['code']; ?></td>
        <td><?php echo $order['Order']['phone']; ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['receive_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['working_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['ready_state']); ?></td>
        <td> <?php //echo $this->Html->link($this->Html->image("cancel.jpg"), array('action' => 'index'), array('escape' => false));?><?php echo $this->Html->link($this->Html->image("edit.jpg"), array('action' => 'edit',$order['Order']['id']), array('escape' => false));/*echo $this->Html->link('Edit', array('action' => 'edit', $order['Order']['id']));*/?></td>
    </tr>
    <?php endforeach; ?>
    <tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

 <?php }else echo"no results found"; ?>


<?php } //endif?>

这是我的控制器动作

function search($options=null){
        $this->set('results',"");
        $this->set('term',"");
        $this->set('rs',0);    

        if ((isset($_GET["SearchTerm"]))||(isset($_GET["options"])) ) {
            $SearchTerm=$_GET["SearchTerm"];
            $options=$_GET["options"];

            if (!$options || !$SearchTerm) {
                $this->Session->setFlash('Please enter something to search for');
            }
            else {
                $SearchArray = array($options." LIKE " => "%".$SearchTerm."%");

                $this->paginate = array('conditions' =>  $SearchArray,'limit'=>2,'convertKeys' => array($options, $SearchTerm));
                $data=$this->paginate('Order');
                $this->set('rs', $data);
                $this->set('term',$SearchTerm);

            }
        }

如您所见,我正在使用Get参数选项和SearchTerm。我希望两个人留在分页链接中。我尝试过在stackoverflow和其他网站上找到的各种修复程序(例如:

$urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));

但我仍然收到以下错误消息:

  

间接修改重载属性   PaginatorHelper :: $ settings无效   [APP \ View \ orders \ search.ctp,第75行

为什么?那解决方案怎么样? (即使我使用了食谱中的wxample

  

$ this-&gt; Paginator-&gt; settings ['paramType'] ='querystring';

我仍然得到同样的错误:你能帮忙解释一下吗?

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的代码,您应该能够替换:

<?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';
?>

<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>

分页中的表单字段 如果您实际上尝试将表单元素放入分页中,则需要稍微更改代码。您需要为搜索选项中的每个项目构建命名参数。然后,在控制器的操作中,您需要检查请求 - >数据和params ['named']版本的搜索,以确保在两种情况下都使用它们。我也会清理代码,使其更具可读性。

首先,您需要使用cake方法来获取数据。它将确保它清除对您的请求等。此外,您还需要考虑作为命名变量传递并在表单中提交的搜索词和过滤器选项。因此,您需要按如下方式更新控制器:

function search($options=null){

    $this->set('results',""); // where is this used ??
    $this->set('rs', 0); 
    $this->set('SearchTerm', '');
    $this->set('FilterBy', '');   
    $this->set('options', array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'));

    if ($SearchTerm = $this->request->data['Order']['SearchTerm']) or $SearchTerm = $this->params['named']['SearchTerm']) {
        if ($FilterBy = $this->request->data['Order']['FilterBy'] or $FilterBy = $this->params['named']['FilterBy'])) {
            $this->paginate = array(
                'conditions' => array($FilterBy." LIKE " => "%".$SearchTerm."%"), 
                'limit' => 2, 
                'convertKeys' => array($FilterBy, $SearchTerm)
            );
            $this->set('rs', $this->paginate('Order'));
            $this->set('SearchTerm', $SearchTerm);
            $this->set('FilterBy', $FilterBy);
        } else {
            $this->Session->setFlash('Please enter something to search for');
        }
    } else {
        $this->Session->setFlash('Please enter something to search for');
    }
}

接下来,我们关注视图。移除此移动:

$options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail');

它不应该在视图中。它属于控制器(现在就是它)。

接下来,清理表单:

<?php
  echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));
  echo $this->Form->input('SearchTerm',array('label' => 'Find:'));
  echo $this->Form->input('FilterBy', array('type'=>'select', 'label'=>'Search:', 'options'=>$options));
  echo $this->Form->end('search'); 
?>  

*请注意,我将options名称更改为FilterBy,以便在控制器中更容易找到。还有其他东西可以在视图中清理。但是,我只会解决与问题相对应的问题。

现在您需要替换此代码:

<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

使用此代码:

</table>
<p>
<?php
  $this->Paginator->options(array('url' => array_merge(array('SearchString' => $SearchString, 'FilterBy' => $FilterBy), $this->passedArgs)));

echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
));
?>  
</p>

<div class="paging">
    <?php echo $this->Paginator->prev('<< ' . __('previous'), array(), null, array('class'=>'disabled'));?>
 |  <?php echo $this->Paginator->numbers();?>
 |  <?php echo $this->Paginator->next(__('next') . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

您的格式不同,当然是为了满足您的需求。但是代码应该按预期工作,搜索术语和过滤器选项应用于分页。

祝你好运,快乐的编码!