开发者

problem with updating in cakephp

hi there i am trying to update userdata which is already saved in database table but some how i t is creating new record instead of updating the existing one. here is my code.

function admin()
{       
    $conditions = "id";
    $recursive = 1;
    $data = $this->User->find('all');
    $counter = $this->User->find('count');
    $this->set('counter', $counter);
    $this->set('combo', $data);

    $username = $this->Session->read('user');
    if ($username)
    {
        $results = $this->User->findByUsername($username);
        $this->set('User', $results['User']);
        $this->set('last_login', $this->Session->read('last_login'));
        if (!empty($this->data))
        {


            $this->data["User"]["access_right"] = implode(",",$this->data["User"]["access_right"]);
            $this->User->set($this->data);
            if ($this->User->validates())
            {                    
               $this->data['User']['password'] = md5($this->data['User']['password']);
               if($this->User->saveAll($this->data, TRUE, array('username','password','first_name','last_name','notes','access_right')))
               {
                  $this->redirect(array('action' => 'admin'));
                  $this->Session->setFlash('User Data Updated.');
               }
               else
               {
                  $this->Session->setFlash('There was a problem while Updating User.');
               }                    
            }
        }
    }
    else
    {
        $this->redirect(array('action' => 'login'));
        exit();
    }        
}

and this is my Model

<?php
class User extends AppModel
{
    var $name = 'User';
    var $validate = array(
        'username'=>array(
                  /* 'alphaNumeric'=>array(
                'rule'=>'alphaNumeric',
  开发者_如何学运维              'required'=>true,
                'message'=>'Alphabets and numbers only'
                ), */
            'between'=>array(
                'rule'=>array('between', 5, 15),
                'message'=>'Between 5 and 15 characters'
            )
        ),
        'password'=>array(
            'rule'=>array('minLength', 8),
            'required'=>true,
            'message'=>'Minimum 8 characters long'
        ),
        'email'=>'email'
    );
}

?>


Creating or updating is controlled by the model's id field. If $Model->id is set, the record with this primary key is updated. Otherwise a new record is created. You may want to check to see if $this->data contains: $this->data['User']['id']. If it does not, that is why it is creating a new record. To fix this, you could do something like:

$this->data['User']['id'] = $this->Session->read('user_id');

That is of course if you are storing it in the session. Otherwise, you may need to update the form in the view to hold the hidden filed for the user id. This is not as secure as a session stored variable however, depending on your security needs of hiding the user id or not.

Happy Coding!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜