stamas
stamas

Reputation: 397

Symfony2 ManyToMany embedded forms

I have a Post and Tag entity in my application, and I need many to many association between them. 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:

/**
* @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. 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. 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. 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

Upvotes: 0

Views: 1690

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20201

You don't need intermediary entity between Post and 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.

Regarding the embedded forms, once you establish Many-To-Many relation between Post and Tag you'll need to use collection field form type. Basically, you'll be saying: "OK, I have a form that has fields of Post which can have many 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.

Upvotes: 4

Related Questions