2014-04-18 36 views
1

这是我简化的问题描述。我有与包含不同的数据类似的列几个表:如何正确地从控制器传递多个列表来查看?

table_one.id, table_one.name, ..., table_one.foo 
table_two.id, table_two.name, ..., table_two.foo 

table_one.foo允许值是 '一', 'B', 'C',

table_two.foo允许值是 'x' 的,'y','z'。

在视图中我有一个表单,允许进入多条记录写入table_one和table_two:

$this->Form->input('TableOne.0.foo', array('type' => 'select')); 
$this->Form->input('TableOne.1.foo', array('type' => 'select')); 
$this->Form->input('TableOne.2.foo', array('type' => 'select')); 
... 
$this->Form->input('TableTwo.0.foo', array('type' => 'select')); 
$this->Form->input('TableTwo.1.foo', array('type' => 'select')); 
$this->Form->input('TableTwo.2.foo', array('type' => 'select')); 

在控制器I构建列表,并将其传递给视图:

$tableOneFooList = array('a', 'b', 'c'); 
$tableTwoFooList = array('x', 'y', 'z'); 
$this->set('foo', $tableOneFooList); 

问题在于我无法设置第二个foo变量,并且$tableOneFooList已填充到所有6个选择框中。我可以在控制器中以不同的方式命名列表,但在视图中需要更多工作才能在验证失败后选择正确的值。是否有一种很好的方式将列表传递给视图,以便如果表单在提交后不验证,则所选值将被保留?也许我不知道一些命名约定?

回答

1

据我所知,表单助手魔法无法区分这两个字段,您必须分别传递列表或使用自定义表单助手。

使用options选项

当分别通过列表,所有你必须使用options option,如果必要的话CakePHP会自动选择基于在请求中传递的值相应的列表条目做。

控制器

$tableOneFoos = array('a', 'b', 'c'); 
$tableTwoFoos = array('x', 'y', 'z'); 
$this->set(compact('tableOneFoos', 'tableTwoFoos')); 

查看

$this->Form->input('TableOne.0.foo', array('type' => 'select', 'options' => $tableOneFoos)); 
$this->Form->input('TableOne.1.foo', array('type' => 'select', 'options' => $tableOneFoos)); 
$this->Form->input('TableOne.2.foo', array('type' => 'select', 'options' => $tableOneFoos)); 

$this->Form->input('TableTwo.0.foo', array('type' => 'select', 'options' => $tableTwoFoos)); 
$this->Form->input('TableTwo.1.foo', array('type' => 'select', 'options' => $tableTwoFoos)); 
$this->Form->input('TableTwo.2.foo', array('type' => 'select', 'options' => $tableTwoFoos)); 

就是这样,渲染与所提交的数据表单时预期应选择的值。

自定义形式是字段名的camelCased plurals变量名助手

形式助手检查(见FormHelper::_optionsOptions()),型号名称是没有考虑到,所以在你的情况下,它会寻找foos,这就是为什么你要结束这一个列表被用于所有输入。

您可以重写该方法并实施使用模型名称的附加检查,以便帮助程序查找像tableOneFoostableTwoFoos这样的变量。

这是一个未经测试!例如:

App::uses('FormHelper', 'View/Helper'); 

class MyFormHelper extends FormHelper { 
    protected function _optionsOptions($options) { 
     $options = parent::_optionsOptions($options); 

     if (!isset($options['options'])) { 
      // this is where the magic happens, an entity name like 
      // `Model_field` is turned into a variable name like 
      // `modelFields` which is then used to lookup the view vars. 
      $entityName = $this->model() . '_' . $this->field(); 
      $varName = Inflector::variable(
       Inflector::pluralize(preg_replace('/_id$/', '', $entityName)) 
      ); 
      $varOptions = $this->_View->get($varName); 

      if (is_array($varOptions)) { 
       if ($options['type'] !== 'radio') { 
        $options['type'] = 'select'; 
       } 
       $options['options'] = $varOptions; 
      } 
     } 

     return $options; 
    } 
} 

然后你可以只通过tableOneFoostableTwoFoos到视图不使用optionstype选项输入:

控制器

$tableOneFoos = array('a', 'b', 'c'); 
$tableTwoFoos = array('x', 'y', 'z'); 
$this->set(compact('tableOneFoos', 'tableTwoFoos')); 

查看

$this->MyForm->input('TableOne.0.foo'); 
$this->MyForm->input('TableOne.1.foo'); 
$this->MyForm->input('TableOne.2.foo'); 

$this->MyForm->input('TableTwo.0.foo'); 
$this->MyForm->input('TableTwo.1.foo'); 
$this->MyForm->input('TableTwo.2.foo'); 

这应该只要有一个HABTM时尚的方式使用没有TableOneFooTableTwoFoo模式工作,因为他们的领域最终会使用相同的变量名,即tableOneFoostableTwoFoos

+0

定制辅助方式,通过工作的方式对我来说:) – bancer

+0

,它也可以用一个单一的输入,比如'$这个 - > MyForm- > input('TableOne.foo');' – bancer

0

让不使这个complex--简单的解决办法是─

<?php 
$tableOneFooList = array('a', 'b', 'c'); 
$tableTwoFooList = array('x', 'y', 'z'); 

echo $this->Form->create(); 
echo $this->Form->input('TableOne.0.foo', array('type' => 'select', 'options' => $tableOneFooList)); 
echo $this->Form->input('TableOne.1.foo', array('type' => 'select', 'options' => $tableOneFooList)); 
echo $this->Form->input('TableOne.2.foo', array('type' => 'select', 'options' => $tableOneFooList)); 
echo $this->Form->input('TableTwo.0.foo', array('type' => 'select', 'options' => $tableTwoFooList)); 
echo $this->Form->input('TableTwo.1.foo', array('type' => 'select', 'options' => $tableTwoFooList)); 
echo $this->Form->input('TableTwo.2.foo', array('type' => 'select', 'options' => $tableTwoFooList)); 
echo $this->Form->end('Submit'); 
?> 
+0

不幸的是,这很麻烦,虽然很简单。我需要设置选项的地方太多了。 – bancer

相关问题