douzhi2012 2014-05-22 03:45
浏览 18
已采纳

如何在行为配置中使用模型属性值Yii

I want create ImageBehavior for uploading and saving images. My behavior have two fields: imagePath and imageField. In my model i'm wrote:

  1. public function behaviors(){
  2. return array(
  3. 'imageBehavior' => array(
  4. 'class' => 'ImageBehavior',
  5. 'imagePath' => 'images/avatar-pics/'.$this->user->username,
  6. 'imageField' => 'avatar',
  7. ),
  8. );
  9. }

but this doesn't worked - i receive path -

images/avatar-pics//image.png

What a solution? Create in behavior field imageFolder and add to config 'imageFolder' => 'user->username'? Thanks.

  • 写回答

3条回答 默认 最新

  • doubi1910 2016-04-22 10:29
    关注

    Sorry for answering own question, but almost two years later build correct vision to this issue:

    1) don't use line

    'images/avatar-pics/'.$this->user->username

    in your behavoir - it throws a notice "Trying to get property of non-object" - good application doesn't thows notices and warnings.

    2) all you need is use

    'images/avatar-pics/' . CHtml::value($this, 'user.username') 
    

    instead. If $this->user is null, value method returns null, so be sure that you have related user to your model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部