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

时间:2011-09-06 08:57:25

标签: cakephp cakephp-1.3 cakephp-appmodel

我有一个用于在我的应用中编辑Venue模型记录的页面。这个页面在某个阶段正在运行,但现在已经破了。

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

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

        )

)

提交给控制器操作的表单和对beforeSave的调用之间的数据可能会发生什么?我应该在哪里调试这个?

感谢

编辑 - 这是$ this->控制器中的数据(实际数据已更改为删除电话号码,地址等)。

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

3 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

也许是一个愚蠢的问题,但是在调用save()方法时是否将控制器$数据的内容传递给模型?

$this->Venue->save($this->data)

答案 2 :(得分:0)

您是否尝试同时将条目保存到类别表中?如果是这样,您可以使用$this->Venue->saveAll($this->data)代替save()。如果您只想保存Venue数据,只需将其传递到save()而不是整个$this->data,如下所示:$this->Venue->save($this->data['Venue']);