Я пытаюсь вставить в соединительную таблицу с именем post_post_category, которая основана на таблицах Post и PostCategory [многие ко многим >], мне удается выполнить вставку, но проблема в том, что когда я добавляю новый пост, он создает новую категорию.
Объект публикации:
class Post
{
...
/**
* @ORM\ManyToMany(targetEntity="PostCategory", cascade={"persist"})
* @ORM\JoinTable(name="post_post_category",
* joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="post_category_id", referencedColumnName="id")}
* )
*/
private $categories;
public function __construct() {
$this->categories = new ArrayCollection();
}
...
public function getCategories(): ?PostCategory
{
return $this->categories;
}
public function addCategory(PostCategory $category): self
{
$this->categories->add($category);
return $this;
}
}
Категория сущности:
class PostCategory
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=60)
*/
private $category_description;
public function getId()
{
return $this->id;
}
public function getCategoryDescription(): ?string
{
return $this->category_description;
}
public function setCategoryDescription(string $category_description): self
{
$this->category_description = $category_description;
return $this;
}
}
Контроллер:
...
$category->setCategoryDescription('New category');
$post->addCategory($category);
$database_manager->persist($post);
$database_manager->flush();
Как вставить существующую категорию в соединительную таблицу?