php - CakePHP: Call to a member function set() on a non-object error -


my controller seems buggy here:

public function addadmin($id) {     $this->user->id = $id;     $this->user->set('role','admin');     $this->user->save(); } 

so throws me error call member function set() on non-object.

actually, want update field 'role' in table column called 'role' , set 'admin'.

can imagine what's wrong? i've seen many tutorials using success here apparently i'm missing something.

ps: i'm cakephp newbie :d

thank in advance!

the user model isn't loaded.

try loading it:

public function addadmin($id) {     $this->loadmodel('user'); // here     $this->user->id = $id;     $this->user->set('role','admin');     $this->user->save(); } 

you don't have load model if you're in controller of model - in case, you're in different controller, either need load model (like above), or access via already-loaded associated model.

so, example, if you're in rolescontroller, access associated 'user' model without having load it:

$this->role->user->set('role', 'admin'); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -