我正在使用 Cakephp 2.x 和 CakeDC 用户插件。我正在尝试通过将以下代码放入我的 AppController 的 beforefilter()
中,使变量 *'editprofile' 和 'profile_area_pic' 可用于所有 Controller - 此代码允许我显示用户个人资料图片并且工作正常,直到您尝试注册用户并给出以下错误:
[PDOException] SQLSTATE[42000]: 语法错误或访问冲突:1064
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“register”附近使用的正确语法。
有人对如何使这些变量可用有更好的了解吗?提前致谢。
/////////////* 使以下变量可用/////////////////////////////////////////////
$this->loadModel('Profile');
$profileedit = $this->Auth->User('id');
$editprofile = $this->Profile->find('first',array(
'conditions'=>array('Profile.user_id'=>$profileedit),
'fields'=>array('id')));
$this->set(compact('editprofile'));
$this->loadModel('Profile');
$profileuse = $this->Auth->User('id');
$profile_area_pic = $this->Profile->find('first',array(
'conditions'=>array('Profile.user_id'=>$profileuse),
'fields'=>array('photo')));
$this->set(compact('profile_area_pic'));
最佳答案
实现您想要做的事情的更好方法是:
function beforeFilter() {
if (!in_array($this->action, array('register','any_other_action',
'here')) {
// Do your authentication or your profile query
}
}
只需尝试根据您在过滤器之前 checkin 的需要进行更改,它一定会对您有所帮助。
关于cakephp - 使变量可用于 CakePHP 2.x 中的所有 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913345/