我在cakephp中有一個名爲AdminsController的控制器。使用addadmin($ id)函數,如您所見,我嘗試將用戶角色更新爲'admin',並使用其用戶標識(變量$ id)在數據庫中找到他。用cakephp更新表字段時出錯
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();
}
}
但這代碼不起作用,但在計算器許多其他CakePHP的用戶告訴我,這是對的方式做這個.. 你knwo什麼可能出錯,或者你能幫助我在任何情況下?
謝謝你提前!
您還沒有提供足夠的信息。請發佈您的管理員和用戶模型(我認爲關係很好),也許數據庫表。 – Nunser 2013-05-10 02:11:08
我的用戶模型:http://pastie.org/private/v4mgm2qzx0wz0th9xmsr7g 和我的管理模式:http://pastie.org/private/go6oysxkrs7uz5mltay8cw – 2013-05-10 07:34:11