2013-10-25 1 views
0

Я новичок cakephp, и мне было приказано использовать версию 1.3. Я не могу понять (и оба руководства, и api docs не говорят), как я мог бы создать ассоциацию HABTM в запросе POST.Cake php multimodel form post параметры

Я пытаюсь создать вино, которое может быть сделано из многих лоз. Например, я создаю «волнообразный» визг, который сделан из винограда «garganega» и «chardonnay».

Как должны быть параметры 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

после сохранения вины, попробуйте инъекционный его идентификатор в массив данных

$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: но это работает в торте 1.3, если вы измените последнюю строку метода saveAssociated на

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