CakePhp - 相关数据不保存(但主模型数据保存) [英] CakePhp - Associated Data not Saving (but main model data does save)

查看:161
本文介绍了CakePhp - 相关数据不保存(但主模型数据保存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个表单在CakePhp使用它的Formhelper。这种形式有两个相关的模型:预订&客人。

So, I have a form in CakePhp using it's Formhelper. There are two associated models in this form: 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?

模型

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

查看 >

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

查看正在处理的SQL时,我没有看到任何建议尝试编辑

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.

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字段:

Try adding the id field of the guest record also in your 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.id');
echo $this -> Form -> input('Guest.0.firstname');
echo $this -> Form -> end("Submit");

?>

这篇关于CakePhp - 相关数据不保存(但主模型数据保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆