Joshua
Joshua

Reputation: 371

Making variables available to all controllers in CakePHP 2.x

I am using Cakephp 2.x and the CakeDC Users plugin. I am trying make variables *'editprofile' and 'profile_area_pic' available to all controllers by placing the below code in my AppController's beforefilter() - This code allows me to display user profile pics and works fine until you try to register a user and gives the following error:

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'register' at line 1.

Has anyone got a better idea of how to make these variables available? Thanks in advance.

///////////// * MAKING BELOW VARIABLES AVAILABLE///////////////////////////////////////////

$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'));

Upvotes: 2

Views: 493

Answers (2)

Tim Joyce
Tim Joyce

Reputation: 4517

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;
}

Upvotes: 1

liyakat
liyakat

Reputation: 11853

A better way to implement what you're trying to do is this:

function beforeFilter() {
  if (!in_array($this->action, array('register','any_other_action',
'here')) {
    // Do your authentication or your profile query
  }
}

just try to change as per your need to check in before filter it will sure help you.

Upvotes: 1

Related Questions