【问题标题】:CakePhp - Associated Data not Saving (but main model data does save)CakePhp - 关联数据不保存(但主要模型数据保存)
【发布时间】:2012-12-28 04:20:21
【问题描述】:

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

保存我的表单数据时,会保存预订信息,但不会保存客人信息。我在下面的 sn-ps 中简化了代码。任何想法我做错了什么?

型号

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

查看

<?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

【问题讨论】:

    标签: php cakephp cakephp-model


    【解决方案1】:

    尝试在您的视图中添加访客记录的 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");
    

    ?>

    【讨论】:

    • 尝试添加 ID 字段。仍然没有运气。同样,对主要预订信息的任何更改都会提交,但对客人的任何更改都不会执行任何操作。郁闷!!
    • 在编辑或添加时是否有任何数据被写入来宾表?
    • 通过此表单保存时不会。直接基于 Guest 模型创建表单可以正确保存和更新,但这里的目标是在上面的预订表单中执行此操作。此外,如前所述,SQL 代码并未表明 cake 甚至试图保存与来宾模型相关的任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2023-03-12
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    相关资源
    最近更新 更多