2012-04-11 3 views
1

Mycakephp 버전은 2.1.1입니다.Cakephp 2.1 관련 객체 수동 저장

내가 수동으로 시려면를 사용하여 관련 모델을 저장하려고

모델

  • 직원 hasOne의 주소
  • 직원이 부서
  • 에 너무

테이블

속한다() 함수

employees(first_name,last_name,age,sex,department_id)

addresses(first_line,second_line,city,state,employee_id)

이제 직원 생성 add.ctp 직원 및 주소 입력을받는 형태

은 내가

$this->Employee->saveAll($this->request->data);

이이 모델을 저장 알고있다하지만
내가 수동으로

를 연결을 저장할

나는 공식 cakephp 문서 here을 통해 가고 있었고 나는 이것과 비슷한 것을 시도했다.

 $this->Employee->saveAll($data, array(
      'fieldList' => array(
      'Employee' => array('first_name','last_name','age','sex','department_id'), 
      'Department' => array('first_line', 'second_line','city','state','employee_id') 
      ) 
     )); 

는 작업 및 발생하지 않는 다음과 같은 오류가

Notice (8): Undefined variable: data [APP\Controller\EmployeesController.php, line 118]

Warning (2): array_keys() expects parameter 1 to be array, null given [CORE\Cake\Model\Model.php, line 1996]

내가 CakePHP의 초보자입니다. 도와주세요.

$this->request->data: Array

(

[Employee] => Array 
    (
     [first_name] => Jack 
     [last_name] => Robert 
     [age] => 32 
     [sex] => 0 
     [Department] => Development 
    ) 

[Address] => Array 
    (
     [first_line] => HSR Layout 
     [second_line] => 1st Cross 
     [city] => Najem 
     [state] => Barel 
    ) 

[Department] => Array 
    (
     [id] => 3 
    ) 

)

+0

'add()'메소드에서 더 많은 것을 보여줄 수 있습니까? '$ data'를 정의하는 곳 은요? –

+0

$ data를 정의하는 법을 말해 줄 수 있습니까? 그것에 대해서는 전혀 모르겠습니다. – maaz

+0

'$ data'를 정의하지 않으면'$ this-> request-> data'를 사용해야합니다. 그러면 폼의 데이터를 얻을 수 있습니다 . –

답변

0

일부 연구를 한 후에 발견했습니다.

 $data = array(
     'Employee' => array(
     'first_name' => $this->request->data['Employee']['first_name'], 
     'last_name'=>$this->request->data['Employee']['last_name'], 
     'age'=>$this->request->data['Employee']['age'], 
     'sex'=>$this->request->data['Employee']['sex'], 
     'department_id'=>$this->request->data['Department']['id'] 
     ), 
     'Adress' => array(
     'first_line' => $this->request->data['Adress']['first_line'], 
     'second_line'=>$this->request->data['Adress']['second_line'], 
     'city'=>$this->request->data['Adress']['city'], 
     'state'=>$this->request->data['Adress']['state'] 
     ) 

    ); 


      $this->Employee->saveAll($data, array('deep' => true)) 

이렇게하면됩니다.

0

것은 fieldList 매개 변수에 그냥 배열을 사용해보십시오 :

$this->Employee->saveAll($this->request->data, array('fieldList' => array('Employee.first_name', 'Employee.last_name', 'Employee.age', 'Employee.sex', 'Department.first_line', 'Department.second_line', 'Department.city', 'Department.state', 'Department.employee_id'))); 

매뉴얼에 따르면, fieldList는 필드의 배열은 당신이 원하는 "기대 저장 허용 ". 나는 다차원 배열을 허용하지 않는다고 생각한다.

$this->request->data은 양식의 모든 입력란입니다. debug($this->request->data)으로 확인할 수 있습니다.

+0

직원 및 박탈을 저장하지 않습니다 – maaz

+0

두 번째 매개 변수없이 시도 했습니까? '$ this-> Employee-> saveAll ($ this-> request-> data)'? 그리고'$ this-> request-> data'를'debug()'로 검사 했습니까? –

+0

$ this-> Employee-> saveAll ($ this-> request-> data) – maaz