title: symfony doctrine persist update/insert
body: <p>I am using symfony with doctrine on a web project. I have a series of entities, one of which is revision, revisions then has a series of one to many relationships. I have am trying to edit a revision and save it as a new row (meaning I want to insert). However, when I persist doctrine updates the existing row instead of inserting. To solve this I explicitly change the revId (the tables primary key) to be different but it still updates. Persist is supposed to know to update if the id is different, so does anyone know why it is updating instead.</p>
  
  <p>Note: I am filling the revision entity with an embedded form containing a series of collection type forms.</p>
  
  <p>Revision Entity</p>
  
  <pre><code>/**
   * @ORM\Entity
   * @ORM\Table(name="policies_revisions")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\RevisionRepo")
   */
  class PoliciesRevisions
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue
   */
  private $revId;
  
  /**
   * @ORM\OneToOne(targetEntity="Policies", inversedBy="revisions", cascade={"persist"})
   * @ORM\JoinColumn(name="policy_id", referencedColumnName="policy_id")
   */
  private $policies;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesApplicability", mappedBy="revision", cascade={"persist"})
   */
  private $applicability;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesAuthority", mappedBy="revision", cascade={"persist"})
   */
  private $authority;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesDefinitions", mappedBy="revision", cascade={"persist"})
   */
  private $definition;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesExclusions", mappedBy="revision", cascade={"persist"})
   */
  private $exclusions;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesInterpretation", mappedBy="revision", cascade={"persist"})
   */
  private $interpretation;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesPolicy", mappedBy="revision", cascade={"persist"})
   */
  private $policy;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesPurposes", mappedBy="revision", cascade={"persist"})
   */
  private $purpose;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesResponsibilities", mappedBy="revision", cascade={"persist"})
   */
  private $responsibilities;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesSanctions", mappedBy="revision", cascade={"persist"})
   */
  private $sanctions;
  
  /**
   * @ORM\OneToMany(targetEntity="PoliciesProcedures", mappedBy="revision", cascade={"persist"})
   */
  private $procedures;
  
  /**
   * @ORM\Column(type="integer")
   */
  private $policyId;
  
  /**
   * @ORM\Column(type="integer")
   */
  private $submittedDatetime;
  /**
   * @ORM\Column(type="integer")
   */
  private $publishedDatetime;
  /**
   * @ORM\Column(type="integer")
   */
  private $createdByUserId;
  /**
   * @ORM\Column(type="integer")
   */
  private $revNum;
  /**
   * @ORM\Column(type="boolean")
   */
  private $published;
  
  public function __construct()
  {
      $this-&gt;applicability = new ArrayCollection();
      $this-&gt;authority = new ArrayCollection();
      $this-&gt;definition = new ArrayCollection();
      $this-&gt;exclusions = new ArrayCollection();
      $this-&gt;interpretation = new ArrayCollection();
      $this-&gt;policy = new ArrayCollection();
      $this-&gt;procedures = new ArrayCollection();
      $this-&gt;purpose = new ArrayCollection();
      $this-&gt;responsibilities = new ArrayCollection();
      $this-&gt;sanctions = new ArrayCollection();
  }
  </code></pre>
  
  <p>Other entities (all follow this form)</p>
  
  <pre><code>/**
   * @ORM\Entity
   * @ORM\Table(name="policies_applicability_two")
   */
  class PoliciesApplicability
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue
   */
  private $appId;
  
  /**
   * @ORM\Column(type="integer")
   */
  private $revId;
  
  /**
   * @ORM\OneToOne(targetEntity="PoliciesRevisions", inversedBy="applicability", cascade={"persist"})
   * @ORM\JoinColumn(name="rev_id", referencedColumnName="rev_id")
   */
  private $revision;
  
  /**
   * @ORM\Column(type="string")
   */
  private $applicabilityText;
  
  public function getAppId()
  {
      return $this-&gt;appId;
  }
  public function setAppId($appId)
  {
      $this-&gt;appId = $appId;
  }
  
  public function getRevId()
  {
      return $this-&gt;revId;
  }
  public function setRevId($revId)
  {
      $this-&gt;revId = $revId;
  }
  
  public function getApplicabilityText()
  {
      return $this-&gt;applicabilityText;
  }
  public function setApplicabilityText($applicabilityText)
  {
      $this-&gt;applicabilityText = $applicabilityText;
  }
  
  public function getRevision()
  {
      return $this-&gt;revision;
  }
  public function setRevision($revision)
  {
      $this-&gt;revision = $revision;
  }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
          $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
          $maxRevNum = $this-&gt;getDoctrine()-&gt;getRepository(PoliciesRevisions::class)-&gt;findMaxRevNum($policyId);
          if ($maxRevNum[0][1] == NULL)
              $task-&gt;setRevNum(1);
          else
              $task-&gt;setRevNum($maxRevNum[0][1] + 1);
  
          $nextRevId = $this-&gt;getDoctrine()-&gt;getRepository(PoliciesRevisions::class)-&gt;findMaxRevId($policyId);
          if ($nextRevId[0][1] != NULL)
              $task-&gt;setRevId($nextRevId[0][1] + 1);
  
          $pol = $this-&gt;getDoctrine()-&gt;getRepository(Policies::class)-&gt;findOneBy(['policyId'=&gt;$policyId]);
          $task-&gt;setPolicyId($policyId);
          $task-&gt;setPolicies($pol);
          $task-&gt;setPublished(0);
          $task-&gt;setPublishedDatetime(0);
  
          foreach ($task-&gt;getApplicability() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
              //print_r($task-&gt;getApplicability());
          }
          foreach ($task-&gt;getAuthority() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getDefinition() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getExclusions() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getInterpretation() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getPolicy() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getProcedures() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getPurpose() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getResponsibilities() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
          foreach ($task-&gt;getSanctions() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
          }
  
          $entityManager-&gt;persist($task);
          $entityManager-&gt;flush();
  </code></pre>
  
  <p>Revision form</p>
  
  <pre><code>$builder-&gt;add('applicability', CollectionType::class, array(
          'entry_type' =&gt; ApplicabilityForm::class,
          'entry_options' =&gt; array('label' =&gt; false),
          'allow_add' =&gt; true,
          'allow_delete' =&gt; true,
          'by_reference' =&gt; true,
          'required' =&gt; false
      ));
  </code></pre>
  
  <p>Other forms (follow the same template)</p>
  
  <pre><code>class ApplicabilityForm extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $MainEntity = $builder-&gt;getData();
      $builder
          -&gt;add('applicabilityText', TextareaType::class, array(
              'required' =&gt; false,
              'label' =&gt; 'Applicability',
              'attr' =&gt; array('class' =&gt; 'col-sm-10')
          ));
  }
  
  /**
   * {@inheritdoc}
   */
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class'=&gt;PoliciesApplicability::class,
      ));
  }
  }
  </code></pre>
  
  <p>UPDATE: I fixed some of the problems, by changing the flush line I stopped the old row from updating which is what I wanted.</p>
  
  <pre><code>$entityManager-&gt;flush($task);
  </code></pre>
  
  <p>The revision table is adding a new row with all the information I would expect. However, no rows are added to tables with a foreign key related to revision. I tried to fix this by making the persistent collections with I used to fill the forms to ArrayCollections, this is how I did it</p>
  
  <pre><code>foreach ($task-&gt;getApplicability() as $i=&gt;$object) {
              $object-&gt;setRevision($task);
              $task-&gt;setApplicability(new ArrayCollection());
              $task-&gt;getApplicability()-&gt;add($object);
          }
  </code></pre>
  
  <p>This did not fix my problem. Sorry for this question being so long, lots of possibly relevant information.</p>