我试图在图片实体和用户实体之间建立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';
}
}
这个错误出现在我身上:
找不到字段"图像"
的映射
我该如何解决
答案 0 :(得分:3)
@Vich\Uploadable
类添加User
,您可以删除该注释。 在表单类型RegistrationFormType
图片不是Vich\Uploadable
类型的情况下,您应该为Image
类创建表单类型,例如cll it 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
))
;
在RegistrationFormType
中引用您的图片类型:
->add('image', new ImageType(),array(...))