0

Hi all I have a 'profiles' table and I need to make this available in my default.ctp view file as I am trying to load profile images. I currently use the $current_user to access the 'users' in the form $current_user['filename']. My user and profile relationships have been set in my models accordingly. If anyone could help I would appreciate it. Thanks in advance.

My AppController's beforeFilter declaration attempt:

$this->set('profile', $this->loadModel('Profile'));
$this->set('profile', $this->User->find('all'));

My default.ctp view file code attempt:

<?php echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$profile['filename']); ?>

Current code:

<?php echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$current_user['filename']); ?>
3
  • What's the issue ? What do you want to do exactly ? Where do the $current_user array come from ?
    – kketch
    Dec 9, 2012 at 23:54
  • 1
    I dont have it clear either what you are asking for. Anyway, you shouldn't set the profile variable twice in the AppController, you are overwriting it... And you shouldn't be able to access to a MOdel directly from a view. Views interacts with Controllers. Just set the variables in there.
    – Alvaro
    Dec 10, 2012 at 9:37
  • I am trying to access the profile picture filename for each individual user as and when they are logged into the system
    – Joshua
    Dec 10, 2012 at 15:27

2 Answers 2

3

You should clarify your requirements. If you want all profiles information on every page, then this is what you need:

public function beforeFilter() {
   $this->loadModel('Profile');
   $profiles = $this->Profile->find('all');
   $this->set('profiles', $profiles);
}


// any view:
foreach($profiles as $profile) : // e.g.
    echo $profile['Profile']['filename'];
endforeach;

but you should specify exactly what data you want to return (id, filename), as otherwise you will be returning huge amounts of data on each request, which will kill performance at any real level.

You should cache this query and the result as it will likely not change very often.

Edit: consider whether to use beforeFilter or beforeRender, depending on your needs.

1
  • I am trying to access the profile picture filename for each individual user as and when they are logged into the system
    – Joshua
    Dec 10, 2012 at 15:22
0

For what it seems you're trying to do, I'd suggest using RequestAction from inside an element.

Check here (specifically the section of requestAction):

http://book.cakephp.org/2.0/en/views.html#elements

Within your element simply call something like:

$profile = $this->requestAction('users/profile/'.$current_user);

echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$profile['Profile']['filename'];

Where 'users/profile/'.$current_user is the url to the method that gets the profile

2
  • I am trying to access the profile picture filename for each individual user as and when they are logged into the system
    – Joshua
    Dec 10, 2012 at 15:23
  • This approach would still work, you just need to know your $current_user which you can get by using $this->Auth->User('id'); and passing that in your requestAction which calls a method that queries your profile model for the identified user (using $this->Profile->find('first').
    – shoesole
    Dec 10, 2012 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.