2013-10-25 20 views
0

我是一個cakephp新手,我被命令使用1.3版本。 我不明白(以及指南和api文檔都沒有告訴它)我如何在POST請求中創建HABTM關聯。蛋糕php多模式表單發佈參數

我正在嘗試創造一種葡萄酒,可以由許多葡萄藤製成。例如,我正在製作一種由「garganega」和「霞多麗」葡萄藤製成的「so」「嗚嗚聲。

POST params應該怎麼樣?

鑑於論文模式

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

編輯
此重載方法是在描述對不起,那是蛋糕2.X
我才意識到1.3沒有saveAssociated方法

編輯2:但它確實工作如果將saveAssociated方法的最後一行更改爲

return parent::saveAll($data, $options);