CakePHP - $this->数据在 Model::save 之前消失

标签 cakephp cakephp-1.3 cakephp-model

我的应用程序中有一个用于编辑 field 模型记录的页面。此页面在某个阶段可以正常工作,但现在已损坏。

在 Controller 操作中,调试 $this->data 给出了预期的表单值数组。但是,在 Venue 模型中,在 beforeSave 中调试 $this->data 仅给出相关 (HABTM) 模型中字段的值,类别:

app/models/venue.php (line 89)
Array
(
    [Category] => Array
        (
            [Category] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 8
                )

        )

)

在提交给 Controller 操作的表单和调用 beforeSave 之间,这些数据可能会发生什么情况?我应该在哪里调试这个?

谢谢

编辑 - 这是 Controller 中 $this->data 中的内容(实际数据已更改以删除电话号码、地址等)。

app/controllers/venues_controller.php (line 63)
Array
(
    [Venue] => Array
        (
            [id] => 19
            [city_id] => 1
            [user_id] => 130
            [name] => Acme Zoo
            [email] => events@acmezoo.org.uk
            [description] => 
Some text...

            [blurb] => Truncated description...
            [contact_id] => 
            [address_1] => Acme Zoo
            [address_2] => Some Road
            [postcode] => PP9 4DD
            [telephone] => 010101010101
            [website] => 
            [latitude] => 55.21222
            [longtitude] => -2.111111
            [featured] => 0
            [active] => 1
            [flagged] => 0
            [smg] => 0
            [smg_custom_icon] => 1
            [listings] => 1
            [send_email] => 0
            [file] => Array
                (
                    [name] => 
                    [type] => 
                    [tmp_name] => 
                    [error] => 4
                    [size] => 0
                )

        )

    [Category] => Array
        (
            [Category] => Array
                (
                    [0] => 3
                    [1] => 6
                    [2] => 10
                )

        )

)

这是我保存数据的代码...

    if (!empty($this->data)) {
        if ($this->Venue->save($this->data)) {
            $this->Session->setFlash('The venue has been saved','success');
            $countryId = $this->Venue->City->field('country_id',array('id'=>$this->data['Venue']['city_id']));
            if (!empty($this->data['Venue']['send_email'])){
                $this->_emailVenue($this->Venue->id,'venue_added',$countryId);
            }
            $this->redirect(array('action' => 'index','city'=>$this->data['Venue']['city_id']));
        } else {
            $this->Session->setFlash('The venue could not be saved. Please, try again.','failure');
        }
    }

最佳答案

我想我找到了一个解决方案,但我真的不确定这是否应该被认为是一个“好的”解决方案。 我在保存之前备份了请求数据,如果失败则恢复。

$temp = $this->request->data;

if ($this->Post->save($this->request->data)) {

}else{
$this->request->data = $temp;
}

关于CakePHP - $this->数据在 Model::save 之前消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7317329/

相关文章:

php - 检测 CakePHP 中的页面名称?

php - 使用cakephp在查找查询中进行区分大小写的搜索

CakePHP:从 View 向布局添加元 nofollow 标签

mysql - 为什么 CakePHP 1.3 会自动填充 "Modified"而不是 "Created"字段?

php - 引用自身的 CakePHP 模型

php - 如何在cakephp的分页中传递参数?

html - cakephp 如何将引导框架 css 样式包含到我的应用程序中?

php - CakePHP - Html->link - 为什么使用 controller=> 和 action=> 而不仅仅是 controller/action

mysql - hasMany 与 "hasMany through"线程消息应用程序的数据库结构