[英]Symfony2 ManyToMany embedded forms
I have a Post and Tag entity in my application, and I need many to many association between them. 我的应用程序中有一个Post and Tag实体,并且它们之间需要多对多的关联。 I think I managed it right, but not enirely sure.
我认为我管理得当,但不确定。 Here are my entities:
这是我的实体:
Post: 帖子:
/**
* @ORM\Table(name="posts")
*/
class Post
{
( ... )
/**
* @ORM\OneToMany(targetEntity="PostTag", mappedBy="post_id")
*/
private $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
( ... )
}
Tag: 标签:
class Tag
{
/**
* @ORM\Column(name="tagname", unique=true, type="string", length=255)
*/
private $tagname;
/**
* @ORM\OneToMany(targetEntity="PostTag", mappedBy="tag_id")
*/
private $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
( ... )
}
I also created a PostTag entity to store these relations: 我还创建了一个PostTag实体来存储这些关系:
/**
* @ORM\Table(name="post_tags")
* @ORM\Entity
*/
class PostTag
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Post", inversedBy="tags")
* @ORM\JoinColumn(name="post_id", referencedColumnName="id")
*/
private $post_id;
/**
* @ORM\ManyToOne(targetEntity="Tag", inversedBy="posts")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
private $tag_id;
( ... )
}
Of course all 3 with appropriate getters/setters. 当然,所有3个都带有适当的吸气剂/吸气剂。 Are the relations okay this way?
这样关系好吗?
I believe I have it right, but now I'm struggling to make an embedded form for the Post entity. 我相信我是对的,但现在我正努力为Post实体制作嵌入式表格。 What I need is, to create a tags field in the PostType, where one could type in tags which are saved in the tags table and the id of both the newly created tag and post in the post_tags table.
我需要的是在PostType中创建一个标签字段,在其中可以键入保存在标签表中的标签以及新创建的标签的ID,并在post_tags表中进行张贴。 I also want the already saved tags to be pickable in another field, that's why I have the entities build this way.
我还希望已经保存的标签在另一个字段中可供选择,这就是为什么我要用这种方式构建实体的原因。
I tried to write this, but got really confused with bad codes, so I don't even try to copy what I had. 我尝试编写此代码,但确实与错误代码混淆,因此我什至不尝试复制自己的代码。 Can someone briefly enlighten me how should I accomplish this?
有人可以简短地启发我,我应该如何做到这一点?
Thanks 谢谢
You don't need intermediary entity between Post
and Tag
. 您不需要
Post
和Tag
之间的中介实体。 I myself struggled to get it working a few months back, but after carefully reading Many-To-Many, Unidirectional , I managed to do it. 我本人努力使它恢复正常工作几个月,但是在仔细阅读《 多对多单向》之后 ,我设法做到了。
The point is that you don't create Many-To-One
and One-To-Many
relations but a single Many-To-Many
. 问题的关键是,你不创建
Many-To-One
和One-To-Many
关系,但单一的Many-To-Many
。
Regarding the embedded forms, once you establish Many-To-Many
relation between Post
and Tag
you'll need to use collection
field form type. 关于嵌入式表单,一旦在
Post
和Tag
之间建立了Many-To-Many
关系,就需要使用collection
字段表单类型。 Basically, you'll be saying: "OK, I have a form that has fields of Post
which can have many Tags
. 基本上,您会说:“好吧,我有一个表单,其中包含
Post
字段,其中可以包含许多 Tags
。
Of course, I would suggest you try managing data manually (persist, update, delete) before trying to make it work with forms. 当然,我建议您先尝试手动管理数据(持久性,更新,删除),然后再使其与表单一起使用。 If you have an error in your model it'll be much more difficult to locate the source of a problem, as forms themselves can be tricky.
如果您的模型有错误,则定位问题的源将更加困难,因为表单本身可能很棘手。
Official Symfony docs have a great article on this, although, I must say, it's a little overwhelming for a Symfony beginner as I was in a time of reading it. Symfony官方文档对此有很棒的文章 ,但是,我必须说,对于Symfony初学者来说,这有点不知所措,就像我在阅读它时一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.