title: ManyToOne ORM association in Doctrine with custom JoinColumn name
body: <p><strong>Update 01.09.2016</strong> I updated my project: I got rid of custom JoinColumn in my entities (now it's generated one, and my project works w/o errors). And I decided to <strong>shorten the question</strong>:</p>
  
  <p><em>Is it good idea to use custom-named JoinColumn in ManyToOne unidirectional association with Symfony 2.7, Doctrine 2.7?</em></p>
  
  <p><em>My case was:</em></p>
  
  <hr>
  
  <p><strong>Many</strong> <em>ipGroup entities</em> (holding many ip addresses) should be associated with <strong>One</strong> <em>ipGroupPrameters entity</em>. (see code below)</p>
  
  <hr>
  
  <p>I'm trying to run the code in 3 variants (see 'the Workflow'), and getting different errors:</p>
  
  <p>1). No errors, <strong>but only</strong> filling the <em>ipGroupParams</em> DB table, and <strong>not inserting any row</strong> in <em>ipGroup</em> table; </p>
  
  <p>2). <em>"Notice: Undefined index: ipGroupId" in vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php at line 685</em></p>
  
  <pre><code>$value = $newValId[$targetClass-&gt;fieldNames[$targetColumn]];
  </code></pre>
  
  <p>which is really strange - $targetClass <strong>has</strong> ipGroupId in fieldNames (as debugger shows);</p>
  
  <p>3). (in this variant it looks like I change the <em>owning</em> side of relation)  <em>DBAL Exception: An exception occurred while executing 'INSERT INTO ipGroup (ipGroupId, ip) VALUES (?, ?)' with params [null, "134.17.26.129"] ipGroupId cannot be null.</em>
  And this is magic, because I apply setIpGroupId method  on $gObj, and it is updated in RAM (I see it in debugger).</p>
  
  <p>The <strong>Workflow</strong> is:</p>
  
  <pre><code>public function createIpGroup()
  {
      $this-&gt;createParamsObj();
      $gObj = new \Bmw\StatBundle\Entity\ipGroup();
      $gObj-&gt;setIpGroupId($this-&gt;groupId);
      $gObj-&gt;setIp($ip);
      ### 1 variant
      $gObj-&gt;setIpGroupParams($this-&gt;paramsObj);
      ### 2 variant
      $gObj-&gt;setIpGroupParams($this-&gt;paramsObj);
      $this-&gt;em-&gt;persist($gObj); // if not rely on cascade persist
      ### 3 variant
      $this-&gt;paramsObj-&gt;addIpGroup($gObj);
      $this-&gt;em-&gt;persist($this-&gt;paramsObj);
      ###
      $this-&gt;em-&gt;flush();
  }
  public function createParamsObj()
  {
      $pObj = new \Bmw\StatBundle\Entity\ipGroupParams();
      $this-&gt;groupId = $this-&gt;container-&gt;get('my.id.generator')-&gt;generate();
      $pObj-&gt;setIpGroupId($this-&gt;groupId);
      $this-&gt;em-&gt;persist($pObj);
      $this-&gt;em-&gt;flush();
      $this-&gt;paramsObj = $pObj;
  }
  </code></pre>
  
  <hr>
  
  <p>Here is initial <strong>entities</strong> code, - then I run <em>doc:gen:entities</em> to automatically create getters and setters:</p>
  
  <pre><code>/**
   * ipGroup
   *
   * @ORM\Table(
   * uniqueConstraints={
   * @UniqueConstraint(name="ip", columns={"ip"}),
   * @UniqueConstraint(name="userId", columns={"userId"}),
   * },
   * indexes={@Index(name="ipGroupId", columns={"ipGroupId"})},
   * )
   * @ORM\Entity
   */
  class ipGroup
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="ipGroupId", type="integer")
       */
      private $ipGroupId;
  
      /**
       * @ORM\ManyToOne(targetEntity="ipGroupParams", inversedBy="ipGroup")
       * @ORM\JoinColumn(name="ipGroupId", referencedColumnName="ipGroupId")
       */
      private $ipGroupParams;
  
      /**
       * @var string
       *
       * @ORM\Column(name="ip", type="string", length=45, nullable=true)
       */
      private $ip;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>/**
   * ipGroupParams
   *
   * @ORM\Table(
   * uniqueConstraints={
   * @UniqueConstraint(name="ipGroupId", columns={"ipGroupId"}),
   * }
   * )
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks()
   */
  class ipGroupParams
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
      /**
       * @ORM\OneToMany(targetEntity="ipGroup", mappedBy="ipGroupParams", cascade={"persist", "remove"}, orphanRemoval=TRUE)
       */
      private $ipGroup;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="ipGroupId", type="integer")
       */
      private $ipGroupId;
  }
  </code></pre>