Reputation: 371
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']); ?>
Upvotes: 0
Views: 2670
Reputation: 17987
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.
Upvotes: 3
Reputation: 105
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
Upvotes: 0