Reputation: 1067
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!
Upvotes: 0
Views: 146
Reputation: 1701
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.
Upvotes: 1
Reputation: 111
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.
Upvotes: 1