title: Doctrine Many to Many Insert
body: <p>i have some promblem. i was research and try all suggest but no one work.</p>
  
  <p>and i end up with :</p>
  
  <blockquote>
    <p><code>Argument 1 passed to Entity\User::addCategories() must be an instance of Entity\Category, string given,</code></p>
  </blockquote>
  
  <p>i have manytomany relationship, user, user_category, and category</p>
  
  <p><strong>user</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  use Gedmo\Mapping\Annotation as Gedmo;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @Entity
   * @Table(name="user")
   */
  class User
  {
  
      /**
       * @Id
       * @Column(type="integer", nullable=false)
       * @GeneratedValue(strategy="AUTO")
       */
      public $id;
  
      /**
       * @Column(type="string", length=255, unique=true, nullable=false)
       */
      public $name;
  
  
      /**
       * @ManyToMany(targetEntity="Entity\Category", inversedBy="user")
       * @ORM\JoinTable(name="user_category")
       */
      public $categories;
  
          public function __construct() {
              $this-&gt;category = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      public function getCategories()
      {
          return $this-&gt;categories;
      }
  
      public function addCategories(Category $category = null)
      {
          $this-&gt;categories = $category;
      }
  
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
      public function getName()
      {
          return $this-&gt;name;
      }
  
  }
  </code></pre>
  
  <p><strong>category</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  use Gedmo\Mapping\Annotation as Gedmo;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @Entity
   * @Table(name="category")
   */
  class Category
  {
  
      /**
       * @Id
       * @Column(type="integer", nullable=false)
       * @GeneratedValue(strategy="AUTO")
       */
      public $id;
  
      /**
       * @Column(type="string", length=255, unique=true, nullable=false)
       */
      public $name;
  
      /**
       * @ManyToMany(targetEntity="Entity\User", mappedBy="category")
       */
      public $user;
  
  
      public function __construct() {
          $this-&gt;user = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      public function getUser()
      {
          return $this-&gt;user;
      }
  
      public function addUser(User $user = null)
      {
          $user-&gt;addCategory($this);
          $this-&gt;user = $user;
      }
  
  
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
      public function getName()
      {
      return $this-&gt;name;
      }
  }
  </code></pre>
  
  <p><strong>Insert function</strong></p>
  
  <pre><code>        // check existence object in database
          $res = $this-&gt;em-&gt;find('Entity\User', $this-&gt;input-&gt;post('id'));
  
          if($res){
              $data = $this-&gt;em-&gt;find('Entity\User', $this-&gt;input-&gt;post('id'));
          }else{
              // create a new User object
              $data = new Entity\User;                
          }
  
  
          $data-&gt;setName($this-&gt;input-&gt;post('name'));
          $data-&gt;addCategories($this-&gt;input-&gt;post('category'));
  
  
          // save the data object to the database
          $this-&gt;em-&gt;persist($data);
  
          $this-&gt;em-&gt;flush();
  </code></pre>
  
  <p>Everything goes fine on get but i'm so confuse for set to work. </p>
  
  <p>thanks for your help.
  sorry for my english.</p>