Symfony将头像字段添加到sfGuardUser模型

时间:2013-03-06 03:54:43

标签: symfony-1.4 image-uploading sfguard

我在symfony有一个项目,我想让我的用户为他们的“头像”字段上传图像。我发现了许多关于如何“扩展”我使用下面的模式的表的帖子:

Member:
  inheritance:
    type:             column_aggregation
    extends:          sfGuardUser  
columns:
    idmember:    { type: integer }
    birthday:    { type: date }
    avatar:      { type: string(255) }
    bio:         { type: string(255) }

列很好地添加到表中,但是当我将窗口小部件更改为sfWidgetFormInputFileEditable时,它会中断。这是Form.class文件:

  $file_src = $this->getObject()->getAvatar();
  if ($file_src == '')
  {
    $file_src = 'default_image.png';
  }

  $this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array(
    'label'     => ' ',
    'file_src'  => '/uploads/avatar/'.$file_src,
    'is_image'  => true,
    'edit_mode' => true,
    'template'  => '<div>%file%<br />%input%</div>',
  ));

和“保存”形式的功能:

if($this->isModified())
    {
      $uploadDir = sfConfig::get('sf_upload_dir');
      $thumbnail = new sfThumbnail(150, 150);
      $thumbnail2 = new sfThumbnail(800, 800);
      if($this->getAvatar())
      {
        $thumbnail->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
        $thumbnail->save($uploadDir.'/avatar/thumbnail/'. $this->getAvatar());
        $thumbnail2->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
        $thumbnail2->save($uploadDir.'/avatar/big/'. $this->getAvatar());
      }
   }  

当我提交表单时,收到此错误消息:

This form is multipart, which means you need to supply a files array as the bind() method second argument.

1 个答案:

答案 0 :(得分:1)

在绑定表单的操作中,您应该使用以下内容:

$form->bind($request->getParamater($form->getName()), $request->getFiles($form->getName()));

因此,您需要将上传的文件作为第二个参数传递给bind方法。