简体   繁体   English

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

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

So, I have a form in CakePhp using it's Formhelper. 所以,我在CakePhp中有一个使用它的Formhelper的表单。 There are two associated models in this form: Booking & Guest. 此表单中有两个相关模型:Booking&Guest。 The database tables seem to be setup correctly since the page populates the values accurately enough through the associations in the model. 数据库表似乎设置正确,因为页面通过模型中的关联足够准确地填充值。

When saving my form data, the Booking information saves but not the Guest information. 保存表单数据时,预订信息会保存,但不会保存访客信息。 I've simplified the code in the snippets below. 我已经简化了下面代码段中的代码。 Any ideas what I'm doing wrong? 我有什么想法我做错了吗?

Model 模型

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
    )
);
}

Controller 调节器

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);
    }
}

View 视图

<?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");
?>

When looking at the SQL that's being processed, I don't see anything to suggest an attempt to edit the Guest table attached to my model. 在查看正在处理的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

Try adding the id field of the guest record also in your view: 尝试在视图中添加访客记录的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");

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM