title: Setup a form entity flield with query builder results
body: <p>Hello everyone I need your help in this issue, the point is:</p>
  
  <p>I have two related entities "Cargo" and "CatVal", see next figure:</p>
  
  <pre><code>CARGO (Key: ShipCargoId)   CATVAL (Key: CatValId)
  +++++++++++++++++++++++++  ++++++++++++++++++++++++
  + ShipCargoId + CargoId +  +CatValId + CatValDesc +
  +++++++++++++++++++++++++  ++++++++++++++++++++++++
  +     3       +     1   +  +    1    + Cargo 1    +
  +++++++++++++++++++++++++  ++++++++++++++++++++++++
  </code></pre>
  
  <p>Ok, in this figure note these related entities have a relation between ShipCargoId.CargoId and CatVal.CatValId and these relation es configure in my Entity file how OneToMany relation from CatVal To Cargo as you can see at next:</p>
  
  <pre><code>namespace OLO\CargoBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Cargo
   *
   * @ORM\Table(name="shipcargos")
   * @ORM\Entity(repositoryClass="OLO\CargoBundle\Entity\CargoRepository")
   * @ORM\HasLifecycleCallbacks()
   */
  class Cargo
  {
  
  /**
   * @ORM\ManyToOne(targetEntity="OLO\CatalogBundle\Entity\CatVal", inversedBy="cargos")
   * @ORM\JoinColumn(name="CargoId", referencedColumnName="CatValId")
   */
  protected $cargoId;
  
  /**
   * @var integer
   *
   * @ORM\Column(name="ShipCargoId", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $shipCargoId;
  
   /**
   * Get id
   *
   * @return integer 
   */
  public function getShipCargoId()
  {
      return $this-&gt;shipCargoId;
  }
  
  /**
   * Set cargoId
   *
   * @param \OLO\CatalogBundle\Entity\CatVal $cargoId
   * @return Cargo
   */
  public function setCargoId(\OLO\CatalogBundle\Entity\CatVal $cargoId = null)
  {
      $this-&gt;cargoId = $cargoId;
  
      return $this;
  }
  
  /**
   * Get cargoId
   *
   * @return \OLO\CatalogBundle\Entity\CatVal 
   */
  public function getCargoId()
  {
      return $this-&gt;cargoId;
  }
  
  }
  
  namespace OLO\CatalogBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * CatVal
   *
   * @ORM\Table(name="catval")
   * @ORM\Entity(repositoryClass="OLO\CatalogBundle\Entity\CatValRepository")
   */
    class CatVal
  {
  /**
   * @ORM\OneToMany(targetEntity="OLO\CargoBundle\Entity\Cargo", 
      mappedBy="cargoId")
   */
  protected $cargos;
  
  /**
   * @var integer
   *
   * @ORM\Column(name="CatValId", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $catValId;
  
  /**
   * @var string
   *
   * @ORM\Column(name="CatValDesc", type="string", length=50)
   */
  private $catValDesc;
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;cargos = new ArrayCollection();
  }
  
  /**
   * Get catValId
   *
   * @return integer 
   */
  public function getCatValId()
  {
      return $this-&gt;catValId;
  }
  
  /**
   * Set catValDesc
   *
   * @param string $catValDesc
   * @return CatVal
   */
  public function setCatValDesc($catValDesc)
  {
      $this-&gt;catValDesc = $catValDesc;
  
      return $this;
  }
  
  /**
   * Get catValDesc
   *
   * @return string 
   */
  public function getCatValDesc()
  {
      return $this-&gt;catValDesc;
  }
  
  /**
   * Add cargos
   *
   * @param \OLO\CargoBundle\Entity\Cargo $cargos
   * @return CatVal
   */
  public function addCargo(\OLO\CargoBundle\Entity\Cargo $cargos)
  {
      $this-&gt;cargos[] = $cargos;
  
      return $this;
  }
  
  /**
   * Remove cargos
   *
   * @param \OLO\CargoBundle\Entity\Cargo $cargos
   */
  public function removeCargo(\OLO\CargoBundle\Entity\Cargo $cargos)
  {
      $this-&gt;cargos-&gt;removeElement($cargos);
  }
  
  /**
   * Get cargos
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getCargos()
  {
      return $this-&gt;cargos;
  }
  }
  </code></pre>
  
  <p>Then, I need to achieve generate a form with entity field which the label be "CatValDesc" and the value be "ShipCargoId"</p>
  
  <p>To achive this I configure a query builder in FormType file, as you can see next:</p>
  
  <pre><code>&gt;add('shipCargos', 'entity', array(
                                                  'class'  =&gt; 'OLOCatalogBundle:CatVal',
                                                  'query_builder' =&gt; function (EntityRepository $er) use($ShipId){
                                                      return $er-&gt;createQueryBuilder('c')
                                                                -&gt;addSelect('c.shpCargoId','p.catValDesc')
                                                                -&gt;leftJoin('c.cargos', 'p', Join::WITH, 'c.catValId = p.cargoId')
                                                                -&gt;where('p.shipId = :id')
                                                                -&gt;setParameter('id', $ShipId);
                                                  },
                                                  'choice_label' =&gt; 'catValDesc',
          ))
  </code></pre>
  
  <p>But I can't make it work, the error that show me now is:</p>
  
  <pre><code>Warning: spl_object_hash() expects parameter 1 to be object, integer given
  
  in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 1189   -
  
   */
  public function isScheduledForInsert($entity)
  {
      return isset($this-&gt;entityInsertions[spl_object_hash($entity)]);
  }
  /**
  
   at ErrorHandler -&gt;handleError ('2', 'spl_object_hash() expects parameter 1 
   to be object, integer given', '/home/ubuntu/workspace/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', '1189', array('entity' =&gt; '45'))
  
  at spl_object_hash ('45') 
  </code></pre>
  
  <p>If I change the addSelect for ->addSelect('c','p') to retrieve intities instead integer value, show me this error:</p>
  
  <pre><code>Neither the property "shipCargoId" nor one of the methods "getShipCargoId()", "shipCargoId()", "isShipCargoId()", "hasShipCargoId()", "__get()" exist and have public access in class "OLO\CatalogBundle\Entity\CatVal".
  </code></pre>
  
  <p>So, any idea how I can make it work like a charm?, best regards</p>