title: Missing an assigned ID for field
body: <p>I'm lost in a complexe relation (for me). I've a entity project for an entity client and you can attach users to a project. When i try to add a project without users, no problem. But, when i try to add a project and users Sf2 display this error :</p>
  
  <p><em>Entity of type Intranet\IntranetBundle\Entity\ProjectUser is missing an assigned ID for field 'project'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.</em></p>
  
  <p>I'm understand it's angry because the project_id is <em>null</em>. But i don't know why it's null.</p>
  
  <p>My Project.php</p>
  
  <p>
  
  <pre><code>namespace Intranet\IntranetBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Gedmo\Mapping\Annotation as Gedmo;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * Project
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Intranet\IntranetBundle\Entity\ProjectRepository")
   */
  class Project
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       * @Assert\NotBlank(
       *      message = "The field cannot be empty" 
       * )
       */
      private $name;
  
      /**
       * @Gedmo\Slug(fields={"name"})
       * @ORM\Column(length=255, unique=true)
       */
      private $slug;
  
      /**
       * @var string
       *
       * @ORM\Column(name="contact_client", type="text", nullable=true)
       */
      private $contactClient;
  
      /**
       * @var string
       *
       * @ORM\Column(name="technologies", type="string", length=255, nullable=true)
       */
      private $technologies;
  
      /**
       * @var string
       *
       * @ORM\Column(name="languages", type="string", length=255, nullable=true)
       */
      private $languages;
  
      /**
       * @var string
       *
       * @ORM\Column(name="extern", type="text", nullable=true)
       */
      private $extern;
  
      /**
       * @var string
       *
       * @ORM\Column(name="url_prod", type="string", length=255, nullable=true)
       */
      private $urlProd;
  
      /**
       * @var string
       *
       * @ORM\Column(name="url_staging", type="text", nullable=true)
       */
      private $urlStaging;
  
      /**
       * @var string
       *
       * @ORM\Column(name="url_dev", type="text", nullable=true)
       */
      private $urlDev;
  
      /**
       * @var string
       *
       * @ORM\Column(name="hosting_company", type="string", length=255, nullable=true)
       */
      private $hostingCompany;
  
      /**
       * @var string
       *
       * @ORM\Column(name="hosting_type", type="string", length=255, nullable=true)
       */
      private $hostingType;
  
      /**
       * @var string
       *
       * @ORM\Column(name="hosting_manager", type="text", nullable=true)
       */
      private $hostingManager;
  
      /**
       * @var string
       *
       * @ORM\Column(name="ssh", type="text", nullable=true)
       */
      private $ssh;
  
      /**
       * @var string
       *
       * @ORM\Column(name="ftp", type="text", nullable=true)
       */
      private $ftp;
  
      /**
       * @var string
       *
       * @ORM\Column(name="db", type="text", nullable=true)
       */
      private $db;
  
      /**
       * @var string
       *
       * @ORM\Column(name="emails", type="text", nullable=true)
       */
      private $emails;
  
      /**
       * @var string
       *
       * @ORM\Column(name="cms", type="text", nullable=true)
       */
      private $cms;
  
      /**
       * @var string
       *
       * @ORM\Column(name="web_services", type="text", nullable=true)
       */
      private $webServices;
  
      /**
       * @var string
       *
       * @ORM\Column(name="comment", type="text", nullable=true)
       */
      private $comment;
  
      /**
       * @ORM\ManyToOne(targetEntity="Intranet\IntranetBundle\Entity\Client", inversedBy="projects")
       * @ORM\JoinColumn(nullable=false)
       */
      private $client;
  
      /**
       * @ORM\OneToMany(targetEntity="Intranet\IntranetBundle\Entity\ProjectUser", mappedBy="project", cascade={"persist"})
       */
      private $projectUsers;
  
      public function __construct()
      {
          $this-&gt;projectUsers = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Project
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set slug
       *
       * @param string $slug
       * @return Client
       */
      public function setSlug($slug)
      {
          $this-&gt;slug = $slug;
  
          return $this;
      }
  
      /**
       * Get slug
       *
       * @return string 
       */
      public function getSlug()
      {
          return $this-&gt;slug;
      }
  
      /**
       * Set contactClient
       *
       * @param string $contactClient
       * @return Project
       */
      public function setContactClient($contactClient)
      {
          $this-&gt;contactClient = $contactClient;
  
          return $this;
      }
  
      /**
       * Get contactClient
       *
       * @return string 
       */
      public function getContactClient()
      {
          return $this-&gt;contactClient;
      }
  
      /**
       * Set technologies
       *
       * @param string $technologies
       * @return Project
       */
      public function setTechnologies($technologies)
      {
          $this-&gt;technologies = $technologies;
  
          return $this;
      }
  
      /**
       * Get technologies
       *
       * @return string 
       */
      public function getTechnologies()
      {
          return $this-&gt;technologies;
      }
  
      /**
       * Set languages
       *
       * @param string $languages
       * @return Project
       */
      public function setLanguages($languages)
      {
          $this-&gt;languages = $languages;
  
          return $this;
      }
  
      /**
       * Get languages
       *
       * @return string 
       */
      public function getLanguages()
      {
          return $this-&gt;languages;
      }
  
      /**
       * Set extern
       *
       * @param string $extern
       * @return Project
       */
      public function setExtern($extern)
      {
          $this-&gt;extern = $extern;
  
          return $this;
      }
  
      /**
       * Get extern
       *
       * @return string 
       */
      public function getExtern()
      {
          return $this-&gt;extern;
      }
  
      /**
       * Set urlProd
       *
       * @param string $urlProd
       * @return Project
       */
      public function setUrlProd($urlProd)
      {
          $this-&gt;urlProd = $urlProd;
  
          return $this;
      }
  
      /**
       * Get urlProd
       *
       * @return string 
       */
      public function getUrlProd()
      {
          return $this-&gt;urlProd;
      }
  
      /**
       * Set urlStaging
       *
       * @param string $urlStaging
       * @return Project
       */
      public function setUrlStaging($urlStaging)
      {
          $this-&gt;urlStaging = $urlStaging;
  
          return $this;
      }
  
      /**
       * Get urlStaging
       *
       * @return string 
       */
      public function getUrlStaging()
      {
          return $this-&gt;urlStaging;
      }
  
      /**
       * Set urlDev
       *
       * @param string $urlDev
       * @return Project
       */
      public function setUrlDev($urlDev)
      {
          $this-&gt;urlDev = $urlDev;
  
          return $this;
      }
  
      /**
       * Get urlDev
       *
       * @return string 
       */
      public function getUrlDev()
      {
          return $this-&gt;urlDev;
      }
  
      /**
       * Set hostingCompany
       *
       * @param string $hostingCompany
       * @return Project
       */
      public function setHostingCompany($hostingCompany)
      {
          $this-&gt;hostingCompany = $hostingCompany;
  
          return $this;
      }
  
      /**
       * Get hostingCompany
       *
       * @return string 
       */
      public function getHostingCompany()
      {
          return $this-&gt;hostingCompany;
      }
  
      /**
       * Set hostingType
       *
       * @param string $hostingType
       * @return Project
       */
      public function setHostingType($hostingType)
      {
          $this-&gt;hostingType = $hostingType;
  
          return $this;
      }
  
      /**
       * Get hostingType
       *
       * @return string 
       */
      public function getHostingType()
      {
          return $this-&gt;hostingType;
      }
  
      /**
       * Set hostingManager
       *
       * @param string $hostingManager
       * @return Project
       */
      public function setHostingManager($hostingManager)
      {
          $this-&gt;hostingManager = $hostingManager;
  
          return $this;
      }
  
      /**
       * Get hostingManager
       *
       * @return string 
       */
      public function getHostingManager()
      {
          return $this-&gt;hostingManager;
      }
  
      /**
       * Set ssh
       *
       * @param string $ssh
       * @return Project
       */
      public function setSsh($ssh)
      {
          $this-&gt;ssh = $ssh;
  
          return $this;
      }
  
      /**
       * Get ssh
       *
       * @return string 
       */
      public function getSsh()
      {
          return $this-&gt;ssh;
      }
  
      /**
       * Set ftp
       *
       * @param string $ftp
       * @return Project
       */
      public function setFtp($ftp)
      {
          $this-&gt;ftp = $ftp;
  
          return $this;
      }
  
      /**
       * Get ftp
       *
       * @return string 
       */
      public function getFtp()
      {
          return $this-&gt;ftp;
      }
  
      /**
       * Set db
       *
       * @param string $db
       * @return Project
       */
      public function setDb($db)
      {
          $this-&gt;db = $db;
  
          return $this;
      }
  
      /**
       * Get db
       *
       * @return string 
       */
      public function getDb()
      {
          return $this-&gt;db;
      }
  
      /**
       * Set emails
       *
       * @param string $emails
       * @return Project
       */
      public function setEmails($emails)
      {
          $this-&gt;emails = $emails;
  
          return $this;
      }
  
      /**
       * Get emails
       *
       * @return string 
       */
      public function getEmails()
      {
          return $this-&gt;emails;
      }
  
      /**
       * Set cms
       *
       * @param string $cms
       * @return Project
       */
      public function setCms($cms)
      {
          $this-&gt;cms = $cms;
  
          return $this;
      }
  
      /**
       * Get cms
       *
       * @return string 
       */
      public function getCms()
      {
          return $this-&gt;cms;
      }
  
      /**
       * Set webServices
       *
       * @param string $webServices
       * @return Project
       */
      public function setWebServices($webServices)
      {
          $this-&gt;webServices = $webServices;
  
          return $this;
      }
  
      /**
       * Get webServices
       *
       * @return string 
       */
      public function getWebServices()
      {
          return $this-&gt;webServices;
      }
  
      /**
       * Set comment
       *
       * @param string $comment
       * @return Project
       */
      public function setComment($comment)
      {
          $this-&gt;comment = $comment;
  
          return $this;
      }
  
      /**
       * Get comment
       *
       * @return string 
       */
      public function getComment()
      {
          return $this-&gt;comment;
      }
  
      /**
       * Set client
       *
       * @param \Intranet\IntranetBundle\Entity\Client $client
       * @return Project
       */
      public function setClient(\Intranet\IntranetBundle\Entity\Client $client)
      {
          $this-&gt;client = $client;
  
          return $this;
      }
  
      /**
       * Get client
       *
       * @return \Intranet\IntranetBundle\Entity\Client 
       */
      public function getClient()
      {
          return $this-&gt;client;
      }
  
      public function addProjectUser(\Intranet\IntranetBundle\Entity\ProjectUser $projectUser)
      {
          $this-&gt;projectUsers[] = $projectUser;
      }
  
      public function removeProjectUser(\Intranet\IntranetBundle\Entity\ProjectUser $projectUser)
      {
          $this-&gt;projectUsers-&gt;removeElement($projectUser);
      }
  
      public function getProjectUsers()
      {
          return $this-&gt;projectUsers;
      }
  }
  </code></pre>
  
  <p>My ProjectUser.php</p>
  
  <p>
  
  <pre><code>namespace Intranet\IntranetBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity
   */
  class ProjectUser
  {
  
      /**
       * @ORM\Id
       * @ORM\ManyToOne(targetEntity="Intranet\IntranetBundle\Entity\Project", inversedBy="projectUsers")
       */
      private $project;
  
      /**
       * @ORM\Id
       * @ORM\ManyToOne(targetEntity="Intranet\UserBundle\Entity\User")
       */
      private $user;
  
      /**
       * @ORM\Column(name="profil", type="smallint")
       */
      private $profil;
  
      public function setProject(\Intranet\IntranetBundle\Entity\Project $project)
      {
          $project-&gt;addProjectUser($this);
          $this-&gt;project = $project;
      }
      public function getProject()
      {
          return $this-&gt;project;
      }
  
      public function setUser(\Intranet\UserBundle\Entity\User $user)
      {
          $this-&gt;user = $user;
      }
      public function getUser()
      {
          return $this-&gt;user;
      }
  
      public function setProfil($profil)
      {
          $this-&gt;profil = $profil;
      }
      public function getProfil()
      {
          return $this-&gt;profil;
      }
  }
  </code></pre>
  
  <p>profil = smallint for the job of the user (1=manager, 2=designer, ...)</p>
  
  <p>My ProjectController (addAction)</p>
  
  <pre><code>public function addAction()
  {
      $project = new Project;
      $form    = $this-&gt;createForm(new ProjectType, $project);
      $request = $this-&gt;get('request');
  
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($project);
              $em-&gt;flush();
  
              foreach ($form-&gt;get('projectUsers')-&gt;getData() as $u) {
                  $u-&gt;setProject($project);
                  $em-&gt;persist($u);
              }
              $em-&gt;flush();
  
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('success', 'The project : '. $project-&gt;getName() .' has been added');
              return $this-&gt;redirect($this-&gt;generateUrl('clients_list'));
          }
      }
      return $this-&gt;render('IntranetIntranetBundle:Project:add_project.html.twig', array(
          'form' =&gt; $form-&gt;createView()
      ));
  }
  </code></pre>
  
  <p>Thanks for your help</p>