蛋糕php多模式表单帖子参数

时间:2013-10-25 15:33:57

标签: cakephp cakephp-1.3

我是一个cakephp新手,我被命令使用1.3版本。 我无法理解(并且指南和api文档都没有告诉它)如何在POST请求中创建HABTM关联。

我正在尝试制作一种可以由许多葡萄藤制成的葡萄酒。例如,我正在创造一种“soave”呜呜声,它是由“garganega”和“chardonnay”葡萄藤制成的。

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`)

1 个答案:

答案 0 :(得分:0)

保存 wine 后,尝试将其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方法的最后一行更改为

,它在蛋糕1.3中有效
return parent::saveAll($data, $options);