gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:28:18 25 4
gpt4 key购买 nike

我有一个实体“评论”,一个“评论”可以关联一个或多个图像。我该如何实现。

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

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

提前致谢

最佳答案

你必须创建一个 ManyToOne Comment 和 Image 实体之间的关系。

阅读更多关于与学说 2 的关联 here .

评论

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

// ...

图片

protected $comment;

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

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

return $this;
}

// ...

然后添加一个collection form使用 ImageFormType 的“类型”(待创建)添加到您的 CommentFormType 字段。

关于php - 使用 VichUploaderBundle 将图像列表设置为实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16725875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com