如何在多对多原则下插入连接 table?
how to insert into junction table when many to many with doctrine?
我正在尝试插入一个名为 post_post_category 的联结 table,它基于 Post 和 PostCategory tables [many to many], 我设法执行插入但是问题是,当我添加一个新的 post 时,它会创建一个新类别。
Post 实体:
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();
如何在联结点 table 中插入现有类别?
要将现有类别添加到 Post,您必须从数据库中检索类别
$category = $em->getRepository('AppBundle:PostCategory')->find($categoryId);
$post->addCategory($category);
我正在尝试插入一个名为 post_post_category 的联结 table,它基于 Post 和 PostCategory tables [many to many], 我设法执行插入但是问题是,当我添加一个新的 post 时,它会创建一个新类别。
Post 实体:
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();
如何在联结点 table 中插入现有类别?
要将现有类别添加到 Post,您必须从数据库中检索类别
$category = $em->getRepository('AppBundle:PostCategory')->find($categoryId);
$post->addCategory($category);