2014-04-18 3 views
1

이것은 간단한 문제 설명입니다. I는 다른 데이터가 포함 유사한 열 여러 테이블 가지고컨트롤러에서 여러 목록을 올바르게 전달하는 방법은 무엇입니까?

table_two.foo 허용 값은,

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

table_one.foo 허락 값이 'A', 'B', 'C'를이다를 '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')); 

하고 뷰에 전달할 :

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

을 문제는 두 번째 foo 변수를 설정할 수없고 모든 선택 상자에 $tableOneFooList이 채워져 있다는 것입니다. 나는 컨트롤러에서 목록의 이름을 다르게 지정할 수는 있지만 유효성 검사가 실패한 후에 올바른 값을 선택하려면보기에서 더 많은 작업이 필요합니다. 양식을 제출 후 유효성을 검사하지 않으면 선택한 값이 유지되도록보기에 목록을 전달하는 좋은 방법이 있습니까? 어쩌면 명명 규칙을 모르겠다.

답변

1

폼 헬퍼 매직이 두 필드를 구별 할 수 없다는 것을 알고 있다면, 목록을 개별적으로 전달하거나 사용자 지정 양식 도우미를 사용해야합니다.

별도로 목록을 통과 할 때 options 옵션

를 사용하면 필요한 CakePHP를 자동으로 요청에 전달 된 값에 따라 적절한 목록 항목을 선택합니다 경우 options option을 사용해야 할 일은. 제출 된 데이터 형식을 렌더링 할 때 예상대로

컨트롤러

$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; 
    } 
} 

당신은 단지 입력에 대해 optionstype 옵션을 사용하지 않고보기로 tableOneFoostableTwoFoos을 전달할 수 :

컨트롤러

$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'); 

자신의 분야 즉, 같은 변수 이름 tableOneFoostableTwoFoos을 끝낼 것입니다 이것은 같은 긴 HABTM 패션 방식으로 사용에는 TableOneFoo 또는 TableTwoFoo 모델이없는 것처럼 작동합니다.

+0

사용자 정의 도우미 방법은 방법으로 나를 위해 :) – bancer

+0

작동 BE- 것 complex-- > input ('TableOne.foo'); ' – bancer

0

만들 수는 없습니다이는 $ this-> MyForm-`처럼 단일 입력으로도 작동, 간단한 솔루션은

<?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

관련 문제