У меня есть проблема, когда doctrine не сохранит ArrayCollection
при сохранении (). У меня есть Post
, и я добавляю Tag
к нему из формы отправки, а затем пытаюсь сохранить его (основы c). На persist () Post
сохраняется, а отношения тегов - нет.
Сброс Post
перед сохранением () показывает, что теги есть.
Чего мне не хватает?
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Post", inversedBy="tags", cascade={"persist"}, fetch="EXTRA_LAZY")
*/
private $post;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\TagType", inversedBy="tags")
* @ORM\JoinColumn(nullable=false)
*/
private $type;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Post", mappedBy="tag")
*/
private $posts;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getPost(): ?Post
{
return $this->post;
}
public function setPost(?Post $post): self
{
$this->post = $post;
return $this;
}
public function getType(): ?TagType
{
return $this->type;
}
public function setType(?TagType $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection|Post[]
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts[] = $post;
$post->addTag($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->contains($post)) {
$this->posts->removeElement($post);
$post->removeTag($this);
}
return $this;
}
public function formChoice()
{
return "<span style='color: {$this->getType()->getColor()};'>{$this->name}</span>";
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
}
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\Column(type="integer")
*/
private $size;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Tag", mappedBy="post", cascade={"persist"}, fetch="EXTRA_LAZY")
*/
private $tags;
/**
* @ORM\Column(type="string", length=255)
*/
private $file;
/**
* @ORM\Column(type="string", length=255)
*/
private $file_orig;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="posts")
*/
private $tag;
/**
* @ORM\OneToOne(targetEntity="App\Entity\ImageSize", mappedBy="post", cascade={"persist", "remove"})
*/
private $imageSize;
public function __construct()
{
$this->tags = new ArrayCollection();
$this->tag = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
$tag->setPost($this);
}
return $this;
}
public function removeTag(Tag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
// set the owning side to null (unless already changed)
if ($tag->getPost() === $this) {
$tag->setPost(null);
}
}
return $this;
}
public function getFile(): ?string
{
return $this->file;
}
public function setFile(string $file): self
{
$this->file = $file;
return $this;
}
public function getFileOrig(): ?string
{
return $this->file_orig;
}
public function setFileOrig(string $file_orig): self
{
$this->file_orig = $file_orig;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTag(): Collection
{
return $this->tag;
}
public function getImageSize(): ?ImageSize
{
return $this->imageSize;
}
public function setImageSize(?ImageSize $imageSize): self
{
$this->imageSize = $imageSize;
// set (or unset) the owning side of the relation if necessary
$newPost = null === $imageSize ? null : $this;
if ($imageSize->getPost() !== $newPost) {
$imageSize->setPost($newPost);
}
return $this;
}
}