can't update an existing record in CakePHP

4k views Asked by At

i write a edit function to update user's info, my code:

if($this->request->is('post')){
                $this->request->data['User']['password']=Security::hash($this->request->data['User']['password'],'sha1', Configure::read('Security.salt'));
                $this->request->data['User']['type'] = '2';
                $this->request->data['User']['username'] = $username;
                //debug($this->request->data);
                if($this->User->validates()){
                    $this->User->set($this->request->data['User']);
                    if ($this->User->save($this->request->data,false)) {
                        $this->Session->setFlash(__('The user has been saved'));
                        $this->redirect($this->root.'index');
                    } else {
                        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                    }
                }
            }

but i check this function: $this->User->exists() never return true, in my case the User table key is username, when i edit username this function can save new info as new record, but when i didn't edit username, a can't save new info, why it work like this and how can i correct my function to edit a record ?

3

There are 3 answers

0
Vayodya Tamari On

Try Following steps

if ($this->User->save($this->request->data,false)) {

        $this->Session->setFlash(__('Recipe Saved!'));

        return $this->redirect(array('action' => 'view_users'));

}

0
AD7six On

Debug your code

if($this->User->validates()){

At no point before this in the code in the question are any of the following done:

  • calling create
  • calling set on the model

As such what's happening here is the code tries to validate an empty model object. Assuming that returns false (as implied by the question) the reason the save "isn't working" is that the code is not calling save at all.

but i check this function: $this->User->exists() never return true

Expected, since there's no call to create, set or setting the User->id property.

Refer to the documentation

It's fine to explicitly call validates but it's also unnecessary. Save calls validation already - as such, if your code worked you'd be running through your validation rules twice.

Use this example from the book for reference:

public function edit($id) {
    if ($this->request->is('post')) {
        if ($this->Recipe->save($this->request->data)) {
            $this->Session->setFlash('Recipe Saved!');
            $this->redirect('/recipes');
        }
    }

    $this->set('recipe', $this->Recipe->findById($id));
}
1
AudioBubble On

Trust me, you almost always want to have an id in your table. Add it and make it as primary key.

Now it would look like this:

if($this->request->is('post')){

    // Think about using AuthComponent:
    // $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
    $this->request->data['User']['password'] = Security::hash($this->request->data['User']['password'],'sha1', Configure::read('Security.salt'));
    $this->request->data['User']['type'] = '2';
    $this->request->data['User']['username'] = $username;
    $this->User->id = $id;

    // Checking if the user exists.
    if(!$this->User->exists()){
        // If not, tell it here.
    }

    if($this->User->validates()){
        if ($this->User->save($this->request->data, false)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect($this->root.'index');
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }else{
        // Something went wrong.
    }
}

If you have username for your primary key, every time it's different, CakePHP will create a new record.

Now, I'll give you my function read with AJAX call, maybe it will help you.

// Function for creating and updating users.
    public function update(){

        // If the request is AJAX (from Ext-JS).
        if($this->request->is('ajax')){

            // Prepare data for CakePHP. $this->request->data['ModelName'] => array of data.
            $this->request->data['User'] = $this->request->data;

            // If there is "id" in data, then it's "edit".
            // If there is no "id" in data, then it's "add".
            if($this->request->data['id'] != null){

                // Setting "id" for user so the CakePHP could use "save", "saveAll", "exits" or 
                // any other function for a particular user.
                $this->User->id = $this->request->data['id'];

                // Checking if the user exists.
                if (!$this->User->exists()) {

                    // If there is no user with a given "id", remove the data (security reasons).
                    // Put the "success" and "message" (message) properties.
                    $this->request->data = array();
                    $this->request->data['success'] = false;
                    $this->request->data['message'] = "No user with this id.";

                    // Return the message to Ext-JS.
                    return new CakeResponse(array('body' => json_encode($this->request->data)));
                }

                // List of fields to save.
                $fieldList = array('id', 'first_name', 'last_name', 'username', 'role_id', 'language_id', 'status', 'last_login', 'created', 'modified');

                // Only if there is an password provided by user, save it.
                if($this->request->data['User']['password'] != ''){
                    array_push($fieldList, 'password');
                }

                // If you are here, then there is a user with a given "id" and you are editing that user.
                if ($this->User->save($this->request->data, true, $fieldList)) {

                    // Remove any data except the "success" and "message" (message) properties (security reasons).
                    $this->request->data = array();
                    $this->request->data['success'] = true;
                    $this->request->data['message'] = "The user has been changed.";
                }else {

                    // If you are here, something went wrong with editing a user. Remove the data (security reasons).
                    // Put the "success" and "message" (message) properties.
                    $this->request->data = array();
                    $this->request->data['success'] = false;
                    $this->request->data['message'] = "Error with editing.";
                }
            }else{

                // If you are here, then you are adding a new user (because there is no "id" in data). Create a new row in table.
                $this->User->create();

                // Try to save the user with data from $this->request->data['User'].
                if ($this->User->save($this->request->data)) {

                    // If you are here, then everything is ok. Remove any data and tell that to Ext-JS with "success" and "message" (message) properties.
                    $this->request->data = array();
                    $this->request->data['success'] = true;
                    $this->request->data['message'] = "The user has been added.";
                }else{

                    // If you are here, something probably went wrong with $this->request->data['User'].
                    // Remove any data (security reasons) and tell that to Ext-JS with "success" and "message" (message) properties.
                    $this->request->data = array();
                    $this->request->data['success'] = false;
                    $this->request->data['message'] = "Something went wrong with adding a user.";
                }
            }

            // Return any data to Ext-JS, despite the success.
            // $this->request->data['success'] => (true | false).
            // $this->request->data['message'] => (message for Ext-JS).
            return new CakeResponse(array('body' => json_encode($this->request->data)));
        }
    }

Now, instead of return new CakeResponse, you send your Session messages to the user.