CakePhp -关联数据不保存(但主模型数据保存)


CakePhp - Associated Data not Saving (but main model data does save)

所以,我有一个表单在CakePhp使用它的Formhelper。这种形式有两个相关的模型:Booking &的客人。数据库表似乎设置正确,因为页面通过模型中的关联足够准确地填充值。

保存表单数据时,保存的是Booking信息,而不是Guest信息。我在下面的代码片段中简化了代码。知道我哪里做错了吗?

class Booking extends AppModel {
public $name = "Booking";
/*Associations Section*/
public $belongsTo = array('Property','Bookingsource');
public $hasMany = array('Payment','Occupant','Bookingfee');
public $hasAndBelongsToMany = array(
    'Addon',
    'Guest' => array(
        'unique' => FALSE
    )
);
}
控制器

public function edit($id) {
    if ($this -> request -> is('post')) {
        if ($this -> Booking -> saveAll($this -> request -> data)) {
            $this -> Session -> setFlash('Booking Edited Successully!', "goodflash");
        } else {
            $this -> Session -> setFlash('Oops Something went wrong!', "badflash");
        }
    } else {
        $this->data = $this -> Booking -> findById($id);
        $this -> set('bookingid', $id);
    }
}
<<p> 视图/strong>
<?php
echo $this->Form->create("Booking", array('type' => 'post'));
echo $this -> Form -> hidden('Booking.id');
echo $this -> Form -> input('Booking.property_id');
echo $this -> Form -> input('Booking.checkin');
echo $this -> Form -> input('Booking.checkout');
echo $this -> Form -> input('Guest.0.firstname');
echo $this -> Form -> end("Submit");
?>

当查看正在处理的SQL时,我没有看到任何迹象表明试图编辑附加到我的模型上的Guest表。

5   UPDATE `cake_bookingsystem`.`bookings` SET `id` = '10000', `property_id` = '01', `checkin` = '2012-11-10', `checkout` = '2012-12-13' WHERE `cake_bookingsystem`.`bookings`.`id` = '10000'       0   0   8
6   COMMIT      0   0   8

尝试在视图中添加来宾记录的id字段:

<?php
echo $this->Form->create("Booking", array('type' => 'post'));
echo $this -> Form -> hidden('Booking.id');
echo $this -> Form -> input('Booking.property_id');
echo $this -> Form -> input('Booking.checkin');
echo $this -> Form -> input('Booking.checkout');
echo $this -> Form -> input('Guest.0.id');
echo $this -> Form -> input('Guest.0.firstname');
echo $this -> Form -> end("Submit");

?>