使用vich_image onetone保存表单

时间:2015-10-16 09:20:36

标签: php symfony vichuploaderbundle

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="mKeyword")
 * @ORM\Entity(repositoryClass="KeywordRepository")
 */
class Keyword {

 /**
     * @ORM\OneToOne(targetEntity="Image",mappedBy="keyword" ,cascade={"all"})
     * @var Image
     */
    private $logo;
}

/**
 * @Vich\Uploadable
 * @ORM\Entity
 * @ORM\Table(name="mKeywordLogo")
 */
class Image {
}

表格

class KeywordType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('title','text')
        ->add('logo',new ImageType())

图像表格

class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('file','vich_image',array(
        'label'=>'logo'
    ));

}

保存表单时 获得

Expected value of type "KeywordsBundle\Entity\Image" for association field "Mea\KeywordsBundle\Entity\Keyword#$logo", got "array" instead.

我在关键字

中添加数组解析器
  public function setLogo($logo)
    {

        if(is_array($logo))
            $logo = reset($logo);
        $this->logo = $logo;
    }

所以得到错误

类型" KeywordsBundle \ Entity \ Image"的预期值对于关联字段" keywordsBundle \ Entity \ Keyword#$ logo",得到" Symfony \ Component \ HttpFoundation \ File \ UploadedFile"代替。

1 个答案:

答案 0 :(得分:1)

我喜欢错误,Form ImageType没有默认值,当我添加它时。保存工作

   /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mea\KeywordsBundle\Entity\Image'
        ));
    }