大家好我有'个人资料'表,我需要在我的default.ctp视图文件中提供这个,因为我正在尝试加载个人资料图片。我目前使用$ current_user以$ current_user ['filename']的形式访问'users'。我的用户和个人资料关系已在我的模型中相应设置。如果有人可以提供帮助,我会很感激。提前谢谢。
我的AppController的beforeFilter声明尝试:
$this->set('profile', $this->loadModel('Profile'));
$this->set('profile', $this->User->find('all'));
我的default.ctp视图文件代码尝试:
<?php echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$profile['filename']); ?>
当前代码:
<?php echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$current_user['filename']); ?>
答案 0 :(得分:3)
您应该澄清您的要求。如果您想在每个页面上显示所有配置文件信息,那么这就是您所需要的:
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;
但是你应该确切地指定你想要返回的数据(id
,filename
),否则你将在每个请求上返回大量数据,这将在任何实际级别上杀死性能
您应该缓存此查询和结果,因为它可能不会经常更改。
修改:根据您的需要,考虑是否使用beforeFilter
或beforeRender
。
答案 1 :(得分:0)
对于你想要做的事情,我建议在元素中使用RequestAction。
点击这里(特别是requestAction的部分):
http://book.cakephp.org/2.0/en/views.html#elements
在你的元素中只需调用类似:
$profile = $this->requestAction('users/profile/'.$current_user);
echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$profile['Profile']['filename'];
其中'users / profile /'.$ current_user是获取个人资料的方法的网址