2013-10-25 4 views
0

저는 cakephp 초보자입니다. 1.3 버전을 사용하도록 명령 받았습니다. POST 요청에서 HABTM 연관을 만들 수있는 방법을 이해할 수 없습니다 (가이드 및 API 문서 모두에서 알 수 없습니다).Cake php multimodel form post 파라미터

나는 많은 포도 나무로 만들어 질 수있는 포도주를 만들고 싶습니다. 예를 들어 나는 "가라 네가 (garganega)"와 "샤르도네 (chardonnay)"포도 나무로 만든 "소라 (soave)"소리를 내고 있습니다.

POST 매개 변수는 어떻게해야합니까?

을 감안할 때 논문 모델

class Wine extends AppModel{ 
    var $hasAndBelongsToMany = array(
     'Vine' => array(
      'className' => 'Vine', 
      'joinTable' => 'wine_vines', 
      'foreignKey' => 'wine_id', 
      'associationForeignKey' => 'vine_id', 
      'with' => 'WineVine', 
     ), 
    ); 
} 

class Vine extends AppModel{ 
    var $hasAndBelongsToMany = array(
     'Wine' => array(
      'className' => 'Wine', 
      'joinTable' => 'wine_vines', 
      'foreignKey' => 'vine_id', 
      'associationForeignKey' => 'wine_id', 
      'with' => 'WineVine', 
     ), 
    );   
} 

class WineVine extends AppModel{ 
    var $name = "WineVine"; 

    public $belongsTo = array("Wine", "Vine"); 
} 

나는 이런 POST 시도 :

Array 
(
    [Wine] => Array 
     (
      [denomination] => Soave DOP 
      [fantasy_name] => 
      [kind] => White 
     ) 

    [Vine] => Array 
     (
      [0] => Array 
       (
        [name] => garganega 
       ) 
      [2] => Array 
       (
        [name] => chardonnay 
       ) 

     ) 

) 

을하지만, 그것은 단지 와인, 포도 나무 테이블에 어떤 삽입을 수행하지 않습니다. 여기 로그입니다 :

2 INSERT INTO `wines` (`denomination`, `fantasy_name`, `kind`, `modified`, `created`) VALUES ('', '', '', '2013-10-25 17:27:14', '2013-10-25 17:27:14')  1  55 
3 SELECT LAST_INSERT_ID() AS insertID  1 1 1 
4 SELECT `WineVine`.`vine_id` FROM `wine_vines` AS `WineVine` WHERE `WineVine`.`wine_id` = 2  0 0 1 
5 SELECT `Vine`.`id`, `Vine`.`name`, `Vine`.`created`, `Vine`.`modified` FROM `vines` AS `Vine` WHERE 1 = 1  5 5 0 
6 SELECT `Wine`.`id`, `Wine`.`denomination`, `Wine`.`fantasy_name`, `Wine`.`kind`, `Wine`.`created`, `Wine`.`modified`, `Wine`.`producer_id`, `WineVine`.`id`, `WineVine`.`wine_id`, `WineVine`.`vine_id`, `WineVine`.`created`, `WineVine`.`modified` FROM `wines` AS `Wine` JOIN `wine_vines` AS `WineVine` ON (`WineVine`.`vine_id` IN (1, 2, 3, 4, 5) AND `WineVine`.`wine_id` = `Wine`.`id`) 

답변

0

와인을 저장 한 후, 데이터 배열로 해당 ID를 주입하려고

$this->data['Wine']['id'] = $this->Wine->id; 

다음 모든 덩굴을 저장하고 업데이트 할 것이다 오버로드 Model::saveAssociated()를 호출 자체적으로 테이블을 조인하십시오. http://bakery.cakephp.org/articles/ccadere/2013/04/19/save_habtm_data_in_a_single_simple_format

편집 :
이 오버로드 방법에 설명되어 죄송합니다, 그 난 그냥 1.3이 더 saveAssociated 방법이 없습니다 실현
케이크 2.x를위한거야

편집 2 :하지만이 작업을 수행 케이크 1.3에서 saveAssociated 메소드의 마지막 행을

return parent::saveAll($data, $options); 
으로 변경하면