cakephp - 使变量可用于 CakePHP 2.x 中的所有 Controller

标签 cakephp cakephp-2.0 cakephp-2.1 cakephp-model

我正在使用 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/

相关文章:

CakePHP CSS 未在 ubuntu 14.04 上加载

.htaccess - 500 内部服务器错误 CakePHP

session - 如何使用cakephp 2.0访问文件routes.php中的 session 变量?

CakePHP 2.1.x - 在 AppController 中运行没有任何模型的查询

cakephp - 将 php 库导入 Cakephp

php - 在没有重写模块的情况下在 cakephp 中找不到 css 文件

jquery - 具有标准 HTML 站点缩小和分组选项的 Assets 管理器

javascript - 如何在 Cakephp 2.3 中将变量传递给 javascript?

php - CakePHP:如何在 View 中使用 Controller::referer()

cakephp - 如何在 Cakephp 中保存特定字段