我有一个
Article
和
Tags
我想创建要编辑的表单
文章
标签
(还有很多其他关系,但目前这并不重要)
在我的例子中,添加新标签非常有效。但如果我想删除一些标签,或编辑现有的,这不会发生,我不会得到一个错误。据我所知
$em->flush()
我还检查了我的引擎——它是InnoDB。我已经创建了外键。
用于编辑我使用的标记
TextType::class
click to see tag input
$builder->add($builder->create('custom_tags', TextType::class, [])->addModelTransformer(function(){/*to array*/}, function(){/*to string*/} );
我把这些实体捆绑起来,因为文件上说:
物品实体:
/**
* Article entity
*
* @ORM\Table(name="Article")
* @ORM\Entity
*/
class Article
{
/**
* Article can have zero or more tags
*
* @OneToMany(targetEntity="Tag", mappedBy="article",
* cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $tags;
public function __construct() {
$this->tags = new ArrayCollection();
}
public function setTags($tags) {
$currTags = $this->getTags();
....
// calculating the difference between currTags and ModifiedTags
//here I got ArrayCollection with new tags entities list, without already deleted tags
$this->tags = $tags;
return $this;
}
public function getTags() {
return $this->tags->matching(Criteria::create()->orderBy(["id" => Criteria::DESC]);
}
...
标记实体
/**
* Tag
*
* @ORM\Table(name="Tag")
* @ORM\Entity
*/
class Tag
{
/**
* @var Article
*
* Many Tags can be present in one article.
*
* @ManyToOne(targetEntity="Article", inversedBy="tags")
* @JoinColumn(name="article_id", referencedColumnName="id")
*/
private $article;
...
谁能给我一些建议,告诉我我错过了什么或给一些链接?我应该手动实现remove\u操作还是显式指定此操作?谢谢