1
/**
 * @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 {
}

form

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

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

image form

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

}

when save form get

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

i add array parser in Keyword

  public function setLogo($logo)
    {

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

so get error

Expected value of type "KeywordsBundle\Entity\Image" for association field "KeywordsBundle\Entity\Keyword#$logo", got "Symfony\Component\HttpFoundation\File\UploadedFile" instead.

4

1 回答 1

1

I Fond error, Form ImageType don't havee defaults, when i add this. save work fine

   /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mea\KeywordsBundle\Entity\Image'
        ));
    }
于 2015-10-17T09:20:04.397 回答