映射

2016-10-29 69 views
2

我試圖使圖像實體和用戶實體就像那個之間的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'; 
    } 
} 

這個錯誤,在我看來:

映射沒有發現場「圖像」

我怎樣才能解決

回答

1
  1. 沒有必要添加@Vich\UploadableUser類,您可以刪除該註釋。
  2. 在表單類型RegistrationFormType圖片Vich\Uploadable類型的沒有,你應該創建Image類的表單類型讓我們CLL它ImageType例如,這添加到它:

    $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(...)) 
    
+1

感謝您的回覆。 – Ld91