简体   繁体   中英

error updating table field with cakephp

i have a controller in cakephp named AdminsController. With the addadmin($id) function,as you can see,i try to update a user's role to 'admin',finding him in the database using his user id (the variable $id).

class AdminsController extends AppController {

public $helpers = array('Html','Form');
var $uses = array('Admin', 'User');



public function index() {
    if(!($this->Auth->user('username') && $this->Auth->user('role') == 'admin')) {
        throw new NotFoundException(__('Unauthorized access.Please login first.'));
        $this->redirect(array('controller' => 'users','action' => 'login'));

    }
}



public function addAdmin($id) {
    $this->loadModel('User');
    $this->User->id = $id;
    $this->User->set('role','admin');
    $this->User->save();
}



}

But this code does not work,though many other cakephp users in stackoverflow have told me that this is the way to do this.. Do you knwo what maybe goes wrong or you can help me in any case?

thank you in advance!

Try this:

public function addAdmin($id) {
    $this->loadModel('User');
    $this->User->id = $id;
    $this->User->saveField('role','admin');
}

If that doesn't work, you are not getting the right ID.

Is 'role' is a field in your users table? And is 'admin' a valid value for that field? If so this should work:

$this->User->id = $id;
$this->User->saveField('role', 'admin');

See here . Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM