4

我正在尝试在 Image 实体和用户实体之间建立 OneToOne 关系,如下所示:

图片 :

    /**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="SocialNetworkBundle\Repository\ImageRepository")
 * @Vich\Uploadable 
 */
class Image {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Get id
     *
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     * 
     * @Vich\UploadableField(mapping="user_image", fileNameProperty="imageName")
     * @Assert\File(
     *     maxSize = "1024k",
     *     mimeTypes = {"image/png", "image/jpeg", "image/jpg"},
     *     mimeTypesMessage = "Please upload a valid PDF or valid IMAGE"
     * )
     * 
     * @var File
     */
    private $imageFile;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     *
     * @var string
     */
    private $imageName;

    /**
     * Set imageName
     *
     * @param string $imageName
     *
     * @return Image
     */
    public function setImageName($imageName) {
        $this->imageName = $imageName;

        return $this;
    }

    /**
     * Get imageName
     *
     * @return string
     */
    public function getImageName() {
        return $this->imageName;
    }

}

用户:

    /**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="UserBundle\Repository\UserRepository") 
 * @Vich\Uploadable
 */
class User extends BaseUser {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, unique=false)
     * @Assert\Length(min=2, max=100)
     */
    private $name;

    /**
     * @ORM\OneToOne(targetEntity="SocialNetworkBundle\Entity\Image", cascade={"persist", "merge", "remove"})
     * @ORM\JoinColumn(name="image_id", referencedColumnName="id")
     * @Assert\Valid()
     */
    private $image;

    /**
     * Set name
     *
     * @param string $name
     *
     * @return User
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Set image
     *
     * @param \SocialNetworkBundle\Entity\Image $image
     *
     * @return User
     */
    public function setImage(\SocialNetworkBundle\Entity\Image $image = null) {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return \SocialNetworkBundle\Entity\Image
     */
    public function getImage() {
        return $this->image;
    }

}

和用户的表单生成器:

    class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        // add your custom field
        $builder->add('name')
             ->add('roles', 'collection', array(
               'type' => 'choice',
               'options' => array(
               'choices' => array(
                   'ROLE_ADMIN' => 'Admin'))))

                ->add('image', VichFileType::class, array(
                    'required' => false,
                    'allow_delete' => true, // not mandatory, default is true
                    'download_link' => true, // not mandatory, default is true
                    ))

                ;

    }

    public function getName()
    {
        return 'user_registration';
    }
}

这个错误在我看来:

找不到字段“图像”的映射

我该如何解决

4

1 回答 1

4
  1. 该类不需要添加@Vich\UploadableUser您可以删除该注释。
  2. 在表单 Type RegistrationFormTypeImage is not of Type中,您应该为类Vich\Uploadable创建一个表单类型,例如,将其添加到其中:ImageImageType

      $builder->add('name')
          ->add('image', VichFileType::class, array(
                'required' => false,
                'allow_delete' => true, // not mandatory, default is true
                'download_link' => true, // not mandatory, default is true
                ))
    
            ;
    
  3. 参考RegistrationFormType您的图像类型:

     ->add('image', new ImageType(),array(...))
    
于 2016-10-30T17:51:48.343 回答