使用VichUploaderBundle将图像列表设置为实体

Ori*_*iam 6 php symfony

我有一个实体"评论","评论"可能有一个或多个图像相关联.我如何实现这一目标.

现在我有了这个(只有一张图片):

/**
 * @Assert\File(
 *     maxSize="1M",
 *     mimeTypes={"image/png", "image/jpeg"}
 * )
 * @Vich\UploadableField(mapping="comment_mapping", fileNameProperty="imageName")
 *
 * @var File $image
 */
protected $image;
Run Code Online (Sandbox Code Playgroud)

提前致谢

Nic*_*ich 2

您必须在 Comment 和 Image 实体之间创建多对一关系。

请在此处阅读有关与原则 2 关联的更多信息。

评论

/**
 * @ORM\ManyToOne(targetEntity="Image", inversedBy="comment")
 */
protected $images;

public function __construct()
{
     $this->images = new ArrayCollection();
}

public function getImages()
{
    return $this->images;
} 

public function addImage(ImageInterface $image)
{
    if (!$this->images->contains($image)) {
        $this->images->add($image);
    }

    return $this;
}

public function removeImage(ImageInterface $image)
{
    $this->images->remove($image);

    return $this;
}

public function setImages(Collection $images)
{
   $this->images = $images;
}

// ...
Run Code Online (Sandbox Code Playgroud)

图像

protected $comment;

public function getComment()
{
   return $this->comment;
}

public function setComment(CommentInterface $comment)
{
    $this->comment = $comment;

    return $this;
}

// ...
Run Code Online (Sandbox Code Playgroud)

然后将集合表单字段添加到您的CommentFormType,其“类型”为ImageFormType(待创建)。