我正在使用Cakephp 2.x和CakeDC Users插件。我正在尝试将变量*'editprofile'和'profile_area_pic'提供给所有控制器,方法是将下面的代码放在我的AppController的beforefilter()
中 - 这段代码允许我显示用户配置文件图片并且工作正常,直到你尝试注册用户并给出以下错误:
[PDOException] SQLSTATE[42000]: Syntax error or access violation: 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'));
答案 0 :(得分:1)
实现您正在尝试执行的操作的更好方法是:
function beforeFilter() {
if (!in_array($this->action, array('register','any_other_action',
'here')) {
// Do your authentication or your profile query
}
}
根据您的需要在尝试更改之前尝试更改,它一定会帮助您。
答案 1 :(得分:1)
function beforeFilter(){
$this->set('editprofile', $this->_edit_profile());
}
function _edit_profile(){
if($this->Auth->user('id')){
$this->loadModel('Profile');
$editprofile = $this->Profile->find('first',array(
'conditions'=>array('Profile.user_id'=>$this->Auth->user('id')), 'fields'=>array('id'))
);
if (!$editprofile || empty($editprofile)) return false;
return $editprofile;
}
return false;
}