apelliciari
apelliciari

Reputation: 8501

Cakephp 2.0: array data in controller (from post) is not editable

I have a simple crud application, and the edit page in my controller is like so:

    function admin_edit($id = null) {

        if (empty($this->data)) {
            $this->data = $this->Product->read(); //product is the model

        } else {
           //its a post request, and $this->data is populated 
            debug($this->data);     

            //i force the id to another id
            $this->data["Product"]["id"] = 115;

            debug($this->data); //the data remains the same, doesnt change.. why?   

           //i will save this later         
        }
}

Both debugs result in this:

before

Array
(
    [Product] => Array
        (
            [id] => 8
            [alias] => ME
            [order] => 80
            [open_close_images] => 1
            [gallery_id] => 8
            [video_id] => 2
        )
)

after:

Array
(
    [Product] => Array
        (
            [id] => 8 //it must be 115 now!!
            [alias] => ME
            [order] => 80
            [open_close_images] => 1
            [gallery_id] => 8
            [video_id] => 2
        )
)

Why this?

In cakephp 1.3 that worked well, i don't understand how it can be possible to "lock" that array.

Upvotes: 2

Views: 2565

Answers (1)

Oldskool
Oldskool

Reputation: 34857

Try setting the id by (re-)setting the Model's id var, like this:

$this->Product->id = 115;

That should update the id properly.

EDIT

If you're trying to update other values, use $this->request->data instead (it's called that since CakePHP 2.0), for example:

$this->request->data['Product']['id'] = 115;

Upvotes: 3

Related Questions