2013-04-10 1 views
0

여러 선택에서 데이터를 저장하려고합니다. 이 데이터는 "요청"hasMany "Requestc"가있는 곳에서 relacioned입니다. foriegnKey는 "REQUEST_ID"입니다여러 선택에서 hasMany 배열을 저장하려고 시도했습니다.

내 컨트롤러 :

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

    $solicitacao = $this->Request->save($this->request->data['Request']); 

    //Verifica se a request foi salva e se sim, salva quais as certidões foram pedidas na tabela requests_certidoes 
    if(!empty($solicitacao)) { 
     $this->request->data['Requestc']['request_id'] = $this->Request->id; 
    // debug($this->request->data); 

     $this->Request->Requestc->saveAll($this->request->data); 
    } 
} 

이 내 데이터가 $this->request->data에서입니다 :

array(
'Request' => array(
    'motivo' => 'Licitação', 
    'nome_licitacao' => '', 
    'data_pregao' => '', 
    'nome_cliente' => '', 
    'outros' => '' 
), 
'Requestc' => array(
    'caminho' => array(
     (int) 0 => '1', 
     (int) 1 => '3' 
    ), 
    'request_id' => '60' 
) 

)

그리고 그 오류입니다 :

오류 : SQLSTATE [42S22] : 열을 찾을 수 없음 : 1054 알 수없는 열 'Array in'필드 l ist '

SQL 쿼리 : INSERT INTO societario. 당신의 관계가 올바르게 설정되어있는 경우

array(
    'Request' => array(
     'motivo' => 'Licitação', 
     'nome_licitacao' => '', 
     'data_pregao' => '', 
     'nome_cliente' => '', 
     'outros' => '' 
    ), 
    'Requestc' => array(
     0 => array(
      'caminho' => '1', 
      // --> optionally add your request_id here 
      //  if you're manually saving Requestc 
      //  AFTER saving Request 
     ), 
     1 => array(
      'caminho' => '3', 
     ) 
    ) 
) 

:이처럼 보이도록 requests_certidoes (caminho, request_id) 가치 (배열, 62)

덕분에 모든

+0

복제본 [saveAll hasMany가 CakePHP 2.0.6에서 작동하지 않음] (http://stackoverflow.com/questions/9667015/saveall-hasmany-doesnt-work-in-cakephp-2-0)을 참조하십시오. -6) – hjpotter92

답변

2

을 위해 당신은 게시 된 데이터를 수정해야 -up, 아마도 request_id를 추가 할 필요는 없습니다.

$data = array(
    'Request' => $this->request->data['Request'], 
    'Requestc' => array(); 
); 

foreach($this->request->data['Requestc']['caminho'] as $val) { 
    $data['Requestc'][] = array(
     'caminho' => $val, 

     // Should NOT be nescessary when using the saveAssociated() 
     // as below 
     //'request_id' => $this->Request->id; 
    ); 
} 

// This should insert both the Request *and* the Requestc records 
$this->Request->saveAssociated($data); 

은 설명서를 참조하십시오 : Saving Related Model Data (hasOne, hasMany, belongsTo)

그러나 Requestc.caminho 저장 Certificatesid이, 이것은 HABTM 관계 것으로 보인다 경우; Request --> HABTM --> Certificate 인 경우 조인 테이블은 certificates_requests이라고하고 request_idcertificate_id 열을 포함해야합니다. Model and Database Conventions

+0

예, 제 'Requestc.caminho'가'Certificates'의'id'를 작성합니다 !!! 테이블 이름과 열을 수정 한 후 코드에서 뭔가를 변경해야합니까? 나는 이해하지 못한다. .. 고마워! –

+0

coloumns의 이름을 바꾸 었는데 이것은'request_id'를 저장하고 있지만'certificate_id'에 아무것도 저장하지 않고 있습니다. 오류 없음 apeears –

+0

고마워요 !!!! foreach 솔루션이 작동했습니다! –

관련 문제