{"action":"created","issue":{"url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/860","repository_url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle","labels_url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/860/labels{/name}","comments_url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/860/comments","events_url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/860/events","html_url":"https://github.com/javiereguiluz/EasyAdminBundle/issues/860","id":131661094,"number":860,"title":"ManyToMany relation is not saved to database","user":{"login":"slmcncb","id":7759682,"avatar_url":"https://avatars.githubusercontent.com/u/7759682?v=3","gravatar_id":"","url":"https://api.github.com/users/slmcncb","html_url":"https://github.com/slmcncb","followers_url":"https://api.github.com/users/slmcncb/followers","following_url":"https://api.github.com/users/slmcncb/following{/other_user}","gists_url":"https://api.github.com/users/slmcncb/gists{/gist_id}","starred_url":"https://api.github.com/users/slmcncb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/slmcncb/subscriptions","organizations_url":"https://api.github.com/users/slmcncb/orgs","repos_url":"https://api.github.com/users/slmcncb/repos","events_url":"https://api.github.com/users/slmcncb/events{/privacy}","received_events_url":"https://api.github.com/users/slmcncb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2016-02-05T14:41:11Z","updated_at":"2016-03-05T08:13:26Z","closed_at":null,"body":"**I can add category in the post but can't add post in the category.**\r\n\r\nPost Entity:\r\n```\r\n<?php\r\n\r\nnamespace AppBundle\\Entity;\r\n\r\nuse Doctrine\\ORM\\Mapping as ORM;\r\nuse Doctrine\\Common\\Collections\\ArrayCollection;\r\n\r\n/**\r\n * Post\r\n *\r\n * @ORM\\Table(name=\"post\")\r\n * @ORM\\Entity(repositoryClass=\"AppBundle\\Repository\\PostRepository\")\r\n */\r\nclass Post\r\n{\r\n    const NUM_ITEMS = 10;\r\n\r\n    /**\r\n     * @var int\r\n     *\r\n     * @ORM\\Column(name=\"id\", type=\"integer\")\r\n     * @ORM\\Id\r\n     * @ORM\\GeneratedValue(strategy=\"AUTO\")\r\n     */\r\n    private $id;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"title\", type=\"string\", length=255)\r\n     */\r\n    private $title;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"slug\", type=\"string\", length=255)\r\n     */\r\n    private $slug;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"summary\", type=\"string\", length=255)\r\n     */\r\n    private $summary;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"content\", type=\"text\")\r\n     */\r\n    private $content;\r\n\r\n    /**\r\n     * @var \\DateTime\r\n     *\r\n     * @ORM\\Column(name=\"publishedAt\", type=\"datetime\")\r\n     */\r\n    private $publishedAt;\r\n\r\n    /**\r\n     * @ORM\\ManyToMany(targetEntity=\"Category\", inversedBy=\"posts\", cascade={\"persist\"})\r\n     * @ORM\\JoinTable(name=\"post_category\")\r\n     */\r\n    private $categories;\r\n\r\n    public function __toString()\r\n    {\r\n        return $this->title;\r\n    }\r\n\r\n    /**\r\n     * Post constructor.\r\n     */\r\n    public function __construct()\r\n    {\r\n        $this->publishedAt = new \\DateTime();\r\n        $this->categories = new ArrayCollection();\r\n    }\r\n\r\n    /**\r\n     * Get id\r\n     *\r\n     * @return integer \r\n     */\r\n    public function getId()\r\n    {\r\n        return $this->id;\r\n    }\r\n\r\n    /**\r\n     * Set title\r\n     *\r\n     * @param string $title\r\n     * @return Post\r\n     */\r\n    public function setTitle($title)\r\n    {\r\n        $this->title = $title;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get title\r\n     *\r\n     * @return string \r\n     */\r\n    public function getTitle()\r\n    {\r\n        return $this->title;\r\n    }\r\n\r\n    /**\r\n     * Set slug\r\n     *\r\n     * @param string $slug\r\n     * @return Post\r\n     */\r\n    public function setSlug($slug)\r\n    {\r\n        $this->slug = $slug;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get slug\r\n     *\r\n     * @return string \r\n     */\r\n    public function getSlug()\r\n    {\r\n        return $this->slug;\r\n    }\r\n\r\n    /**\r\n     * Set summary\r\n     *\r\n     * @param string $summary\r\n     * @return Post\r\n     */\r\n    public function setSummary($summary)\r\n    {\r\n        $this->summary = $summary;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get summary\r\n     *\r\n     * @return string \r\n     */\r\n    public function getSummary()\r\n    {\r\n        return $this->summary;\r\n    }\r\n\r\n    /**\r\n     * Set content\r\n     *\r\n     * @param string $content\r\n     * @return Post\r\n     */\r\n    public function setContent($content)\r\n    {\r\n        $this->content = $content;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get content\r\n     *\r\n     * @return string \r\n     */\r\n    public function getContent()\r\n    {\r\n        return $this->content;\r\n    }\r\n\r\n    /**\r\n     * Set publishedAt\r\n     *\r\n     * @param \\DateTime $publishedAt\r\n     * @return Post\r\n     */\r\n    public function setPublishedAt($publishedAt)\r\n    {\r\n        $this->publishedAt = $publishedAt;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get publishedAt\r\n     *\r\n     * @return \\DateTime \r\n     */\r\n    public function getPublishedAt()\r\n    {\r\n        return $this->publishedAt;\r\n    }\r\n\r\n    /**\r\n     * Add categories\r\n     *\r\n     * @param \\AppBundle\\Entity\\Category $categories\r\n     * @return Post\r\n     */\r\n    public function addCategory(\\AppBundle\\Entity\\Category $categories)\r\n    {\r\n        $this->categories[] = $categories;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Remove categories\r\n     *\r\n     * @param \\AppBundle\\Entity\\Category $categories\r\n     */\r\n    public function removeCategory(\\AppBundle\\Entity\\Category $categories)\r\n    {\r\n        $this->categories->removeElement($categories);\r\n    }\r\n\r\n    /**\r\n     * Get categories\r\n     *\r\n     * @return \\Doctrine\\Common\\Collections\\Collection \r\n     */\r\n    public function getCategories()\r\n    {\r\n        return $this->categories;\r\n    }\r\n}\r\n```\r\n\r\nCategory Entity:\r\n\r\n```\r\n <?php\r\n\r\nnamespace AppBundle\\Entity;\r\n\r\nuse Doctrine\\ORM\\Mapping as ORM;\r\nuse Doctrine\\Common\\Collections\\ArrayCollection;\r\n\r\n/**\r\n * Category\r\n *\r\n * @ORM\\Table(name=\"category\")\r\n * @ORM\\Entity(repositoryClass=\"AppBundle\\Repository\\CategoryRepository\")\r\n */\r\nclass Category\r\n{\r\n    /**\r\n     * @var int\r\n     *\r\n     * @ORM\\Column(name=\"id\", type=\"integer\")\r\n     * @ORM\\Id\r\n     * @ORM\\GeneratedValue(strategy=\"AUTO\")\r\n     */\r\n    private $id;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"name\", type=\"string\", length=255)\r\n     */\r\n    private $name;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"slug\", type=\"string\", length=255)\r\n     */\r\n    private $slug;\r\n\r\n    /**\r\n     * @var string\r\n     *\r\n     * @ORM\\Column(name=\"content\", type=\"text\")\r\n     */\r\n    private $content;\r\n\r\n    /**\r\n     * @ORM\\ManyToMany(targetEntity=\"Post\", mappedBy=\"categories\", cascade={\"persist\"})\r\n     */\r\n    private $posts;\r\n\r\n    public function __toString()\r\n    {\r\n        return $this->name;\r\n    }\r\n\r\n    /**\r\n     * Constructor\r\n     */\r\n    public function __construct()\r\n    {\r\n        $this->posts = new ArrayCollection();\r\n    }\r\n\r\n    /**\r\n     * Get id\r\n     *\r\n     * @return integer \r\n     */\r\n    public function getId()\r\n    {\r\n        return $this->id;\r\n    }\r\n\r\n    /**\r\n     * Set name\r\n     *\r\n     * @param string $name\r\n     * @return Category\r\n     */\r\n    public function setName($name)\r\n    {\r\n        $this->name = $name;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get name\r\n     *\r\n     * @return string \r\n     */\r\n    public function getName()\r\n    {\r\n        return $this->name;\r\n    }\r\n\r\n    /**\r\n     * Set slug\r\n     *\r\n     * @param string $slug\r\n     * @return Category\r\n     */\r\n    public function setSlug($slug)\r\n    {\r\n        $this->slug = $slug;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get slug\r\n     *\r\n     * @return string \r\n     */\r\n    public function getSlug()\r\n    {\r\n        return $this->slug;\r\n    }\r\n\r\n    /**\r\n     * Set content\r\n     *\r\n     * @param string $content\r\n     * @return Category\r\n     */\r\n    public function setContent($content)\r\n    {\r\n        $this->content = $content;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Get content\r\n     *\r\n     * @return string \r\n     */\r\n    public function getContent()\r\n    {\r\n        return $this->content;\r\n    }\r\n\r\n    /**\r\n     * Add posts\r\n     *\r\n     * @param \\AppBundle\\Entity\\Post $posts\r\n     * @return Category\r\n     */\r\n    public function addPost(\\AppBundle\\Entity\\Post $posts)\r\n    {\r\n        $this->posts[] = $posts;\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Remove posts\r\n     *\r\n     * @param \\AppBundle\\Entity\\Post $posts\r\n     */\r\n    public function removePost(\\AppBundle\\Entity\\Post $posts)\r\n    {\r\n        $this->posts->removeElement($posts);\r\n    }\r\n\r\n    /**\r\n     * Get posts\r\n     *\r\n     * @return \\Doctrine\\Common\\Collections\\Collection \r\n     */\r\n    public function getPosts()\r\n    {\r\n        return $this->posts;\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/comments/192605475","html_url":"https://github.com/javiereguiluz/EasyAdminBundle/issues/860#issuecomment-192605475","issue_url":"https://api.github.com/repos/javiereguiluz/EasyAdminBundle/issues/860","id":192605475,"user":{"login":"javierrodriguezcuevas","id":4667395,"avatar_url":"https://avatars.githubusercontent.com/u/4667395?v=3","gravatar_id":"","url":"https://api.github.com/users/javierrodriguezcuevas","html_url":"https://github.com/javierrodriguezcuevas","followers_url":"https://api.github.com/users/javierrodriguezcuevas/followers","following_url":"https://api.github.com/users/javierrodriguezcuevas/following{/other_user}","gists_url":"https://api.github.com/users/javierrodriguezcuevas/gists{/gist_id}","starred_url":"https://api.github.com/users/javierrodriguezcuevas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/javierrodriguezcuevas/subscriptions","organizations_url":"https://api.github.com/users/javierrodriguezcuevas/orgs","repos_url":"https://api.github.com/users/javierrodriguezcuevas/repos","events_url":"https://api.github.com/users/javierrodriguezcuevas/events{/privacy}","received_events_url":"https://api.github.com/users/javierrodriguezcuevas/received_events","type":"User","site_admin":false},"created_at":"2016-03-05T08:13:26Z","updated_at":"2016-03-05T08:13:26Z","body":"You need to set by_reference option to false in collection form in Category:\r\n\r\n`- { property: 'posts', type_options: { by_reference: false} }`\r\n\r\nand also as @Pierstoval said modify Category entity: \r\n\r\n```\r\n// Category.php\r\n\r\n    /**\r\n     * Add posts\r\n     *\r\n     * @param \\AppBundle\\Entity\\Post $posts\r\n     * @return Category\r\n     */\r\n    public function addPost(\\AppBundle\\Entity\\Post $posts)\r\n    {\r\n        if (!$this->posts->contains($posts)) {\r\n            $this->posts[] = $posts;\r\n            $posts->addCategory($this);\r\n        }\r\n\r\n        return $this;\r\n    }\r\n\r\n    /**\r\n     * Remove posts\r\n     *\r\n     * @param \\AppBundle\\Entity\\Post $posts\r\n     */\r\n    public function removePost(\\AppBundle\\Entity\\Post $posts)\r\n    {\r\n        $this->posts->removeElement($posts);\r\n        $posts->removeCategory($this);\r\n    }\r\n\r\n```\r\n\r\nthis is said in documentation http://symfony.com/doc/current/cookbook/form/form_collections.html\r\n\r\n> A second potential issue deals with the Owning Side and Inverse Side of Doctrine relationships. In this example, if the \"owning\" side of the relationship is \"Task\", then persistence will work fine as the tags are properly added to the Task. However, if the owning side is on \"Tag\", then you'll need to do a little bit more work to ensure that the correct side of the relationship is modified.\r\n> \r\n> The trick is to make sure that the single \"Task\" is set on each \"Tag\". One easy way to do this is to add some extra logic to addTag(), which is called by the form type since by_reference is set to false:\r\n\r\nand "}}