开发者

CakePHP - $this->data disappears before Model::save

I have a page for editing records of the Venue model in my app. This page was working at some stage, but is now broken.

in the controller action, debugging $this->data gives the expected array of form values. However, in the Venue model, debugging $this->data in beforeSave gives only the values for fields from a related (HABTM) model, Category:

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

        )

)

What could be happening to this data between the form being submitted to the controller action, and the call to beforeSave? Where should I be looking to debug this?

THanks

Edit - here's what's in $this->data in the controller (actual data changed to remove phone numbers, addresses etc).

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开发者_如何学JAVA] => 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
                )

        )

)

And here's my code to save the data...

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


I think i found a solution to this but I am really unsure if this should be considered a "good" solution. I backup the request data before the save and then restore it if it fails.

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

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

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


Maybe a stupid question, but do you pass the content of controller $data to the model when you call the save() method ?

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


Are you trying to save an entry to the categories table at the same time? If so, you can use $this->Venue->saveAll($this->data) instead of save(). If you just want to save the Venue data, just pass that in to save() instead of the entire $this->data like this: $this->Venue->save($this->data['Venue']);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜