3

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

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

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

        )

)

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

谢谢

编辑 - 这是控制器中 $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');
        }
    }
4

3 回答 3

1

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

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

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

}else{
$this->request->data = $temp;
}
于 2012-06-05T07:37:40.583 回答
0

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

$this->Venue->save($this->data)
于 2011-09-06T09:49:30.743 回答
0

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

于 2011-09-06T14:55:29.443 回答