0

这是我用 cakePHP 创建的第一个页面,我有以下问题/场景:

我正在使用不同的模型创建一个高分页面,因为给定的数据库在不同的表中提供了信息。我正在使用分页和带有两个组合框的表单来允许选择特殊属性。要设置过滤条件,我使用 beforeFilter() 函数并动态设置条件(和连接)。所有条件也存储在会话中,因为它们保留用于分页。这一切都很好。

我现在的问题如下:当我提交表单时,结果是正确的,并且组合框是用我刚刚选择的选项预先选择的。当我现在切换到 page2/3/... 时,组合框的选择很松散,并且处于空选项中。

如何通过分页保持选择?

我的控制器 beforeFilter()-function

// Highscorefilter
public function beforeFilter()
{
  parent::beforeFilter();

  // Dropdown für Rassen
  $this->set('racelist', $this->Race->getRaceList());

  // Dropdown für Galaxien
  $this->set('galaxylist', $this->Galaxy->getGalaxyList());


  // Initialisierung
  $filter_conditions = array();
  $joins = array();
  $filter = FALSE;

  if($this->request->is('post'))
  {

    // Um die Filter richtig zu erkennen brauchen wir den Modelnamen :/
    switch($this->action)
    {
      case "imperium":
        $model = "HighscoreImperiumUser";
        break;
      case "quest":
        $model = "HighscoreQuestUser";
        break;
      case "war":
        $model = "HighscoreWarUser";
        break;
      case "trade":
        $model = "HighscoreTradeUser";
        break;        
      case "overall":
      default:
        $model = "HighscoreOverallUser";
        break;
    }    

    // Filter auslesen und setzen

    $data = $this->request->data;

    if(isset($data[$model]['race_id']) && !empty($data[$model]['race_id']))
    {
      // WHERE-Bedingung race_id setzen
      $tmpstr = sprintf("%s.race_id", $model);
      $filter_conditions[$tmpstr] = $data[$model]['race_id'];

      // Joins setzten wir in diesem Fall auf ein leeres array. Das wird ggf. nachher überschrieben
      $joins = array();
    }

    if(isset($data[$model]['gala']) && !empty($data[$model]['gala']))
    {
      // WHERE-Bedingung gala setzen
      $tmpstr = sprintf("ug.gala", $model);
      $filter_conditions[$tmpstr] = $data[$model]['gala'];        

      // WHERE-Bedingung für Join bauen und Join konstruieren
      $tmpstr = sprintf("ug.userid = %s.id", $model);
      $join = array( 'alias' => 'ug',
                     'table' => 'user_gala',
                     'type' => 'INNER',
                     'conditions' => $tmpstr);
      $joins[] = $join;

    }

    // Filterinformationen für Seitenwechsel in Session schreiben
    $this->Session->delete('_indexConditions');
    $this->Session->delete('_indexJoins');
    $this->Session->write('_indexConditions', $filter_conditions);
    $this->Session->write('_indexJoins', $joins);

    $filter = TRUE;

  }
  else if(!empty($this->request->params['named']['page']))
  { // Filterinformationen aus der Session holen
    $filter_conditions = (array)$this->Session->read('_indexConditions');
    $joins = (array)$this->Session->read('_indexJoins');

    $filter = TRUE;
  }

  if($filter)
  {      
    // set filter conditions of each pagination model
    $this->paginate = Hash::insert($this->paginate, '{s}.conditions', $filter_conditions);
    $this->paginate = Hash::insert($this->paginate, '{s}.joins', $joins);           
  }
}

“真正的”控制器功能之一(它们都是直截了当的,只是使用不同的模型)

// Overall (Default) Highscore
public function overall()
{
  // Paginate der Highscore
  $highscoreusers = $this->paginate('HighscoreOverallUser');
  $this->set('highscoreusers', $highscoreusers);

  // Variablen für View setzen
  $this->set('model', 'HighscoreOverallUser');
  $this->set('title', __d('highscore', 'Overall score'));

  // Index-View benutzen
  $this->render('index');
}    

我的观点-表格

  = $this->Form->create($model, array('type' => 'post', 'url' => array('action' => $this->request['action'], 'controller' => $this->request['controller'])))
  %table
    %tr
      %td{:width => "200px"}
        = $this->Form->label('race_id', __d('highscore', 'Only show race:'))
      %td{:width => "100px"}
        = $this->Form->select('race_id', $racelist, array('legend' => false, 'class' => 'select', 'empty' => __d('highscore', 'All races') ))
      %td{:width => "200px"}
        = $this->Form->label('gala', __d('highscore', 'Only show players in galaxy:'))
      %td{:width => "100px"}
        = $this->Form->select('gala', $galaxylist, array('legend' => false, 'class' => 'select', 'empty' => __d('highscore', 'All galaxies') ))

  = $this->Form->end(__d('highscore', 'Activate filter'))
4

1 回答 1

0

问题是在使用分页器切换页面后,cakePHP 的数据值(例如“data[HighscoreOverallUser][race_id]”)没有设置。

So they have to be set manually. The following code worked for me. Sure you have to be careful not to override set data, but as far as I can say now after using the paginator the data is always not set

if(!$this->data->HighscoreOverallUser)
{
  $data['HighscoreOverallUser']['race_id'] = $filter_conditions['HighscoreOverallUser.race_id'];
  $this->data = $data;
}
于 2013-07-21T09:24:36.863 回答