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

Php 使用VichUploaderBundle将图像列表设置为实体,php,symfony,Php,Symfony,我有一个实体“Comment”,一个“Comment”可以有一个或多个相关的图像。 我如何做到这一点 现在我有了这个(仅一张图片): 提前感谢您必须在您的评论和图像实体之间建立关系 阅读更多关于与条令2的关联 评论 /** * @ORM\ManyToOne(targetEntity="Image", inversedBy="comment") */ protected $images; public function __construct() { $this->image

我有一个实体“Comment”,一个“Comment”可以有一个或多个相关的图像。 我如何做到这一点

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

提前感谢

您必须在您的评论和图像实体之间建立关系

阅读更多关于与条令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;
}

// ...
图像

protected $comment;

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

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

    return $this;
}

// ...

然后在您的CommentFormType中添加一个“type”为ImageFormType(待创建)的字段。

请查看我的评论,如果合适,请接受它,否则请评论您不理解的内容,然后我将更新我的答案。是的,但在这种情况下,我必须创建一个新的实体图像。我想知道是否可以只使用注释实体和VichUploaderBundleIt,而不向注释实体添加多个图像属性是不可能的。。。但那是错的。啊,好的,谢谢,但那太疯狂了。有时你知道图像的数量,但有时不知道。
protected $comment;

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

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

    return $this;
}

// ...