-1

大家好,我正在尝试在每个用户登录系统时访问他们的个人资料图片文件名。我有一个“配置文件”表,当我尝试加载配置文件图像时,我需要在我的 default.ctp 视图文件中提供它。

我目前使用在我的 appController 的 beforefilter 中设置的 $current_user 变量以 $current_user['filename'] 的形式访问'users'。我的用户和个人资料关系已在我的模型中相应地设置。如果有人可以提供帮助,我将不胜感激。提前致谢。

我的 AppController 的 beforeFilter 声明:

$this->loadModel('Profile');
    $profiles = $this->Profile->find('all');
    $this->set('profiles', $profiles);

我的 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']); ?>

我需要能够访问当前用户的配置文件模型文件名字段,以显示他们已经保存到文件名字段的图片。它将在用户登录时调用,我想显示此用户的个人资料图片。

在 default.ctp 视图中,我使用下面的代码,但我需要针对每个用户的个人资料图片进行调整。

foreach($profiles as $profile) : 
    echo $current_user['Profile']['filename'];
endforeach

;

4

1 回答 1

2

Joshua, first of all, instead of posting again try keeping your thread all together (you can edit your post, and this helps others who are searching for the same answer).

How to make a Model available in a View Cakephp 2.x

Again, I'd recommend doing this with an view element and requestAction, but as you didn't accept that answer last time, I'll work with what you are doing here:

If the user is already authenticated, you should have some information about them that you can check in:

 $this->Auth->User();

So, I'd re-work your code as follows:

$this->loadModel('Profile');
$user_id = $this->Auth->User('id');
$profile = $this->Profile->find('first', array('conditions'=>array('Profile.user_id'=>$user_id), 'fields'=>array('filename'));
$this->set(compact('profile'));

Now in your default.ctp file you should be able to call the following:

echo $this->Html->image('/uploads/profile/filename/thumb/small/'.$profile['Porfile']['filename']);
于 2012-12-10T16:42:22.523 回答