title: Doctrine join error "Undefined index: product in ObjectHydrator.php" (Symfony2)
body: <p>I am trying to learn Symfony2 and currently at "8: Entity Relationships/Associations (Joining to Related Records)" of "The Book". Just coded along with the examples (triple checked my code) but then I got this error:</p>
  
  <blockquote>
    <p>Notice: Undefined index: product in C:\My\Path\vendor\doctrine\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 95</p>
  </blockquote>
  
  <p>This is the my code:</p>
  
  <pre><code>//src\Acme\StoreBundle\Repository\ProductRepository.php
  public function findOneByIdJoinedToCategory($id)
  {
      $query = $this-&gt;getEntityManager()
          -&gt;createQuery('SELECT p, c 
              FROM AcmeStoreBundle:Product p 
              JOIN p.category c 
              WHERE p.id = :id')
          -&gt;setParameter('id', $id);
  
      try {
          return $query-&gt;getSingleResult();
      } catch (\Doctrine\ORM\NoResultException $e) {
          return null;
      }
  }
  
  //src\Acme\StoreBundle\Controller\DefaultController.php
  public function showAction($id)
  {
      $product = $this-&gt;getDoctrine()
          -&gt;getRepository('AcmeStoreBundle:Product')
          -&gt;findOneByIdJoinedToCategory($id);
  
      if (!$product) {
          throw $this-&gt;createNotFoundException('No product found for id: '.$id);
      }
  
      $category = $product-&gt;getCategory();
  
      return $this-&gt;render('AcmeStoreBundle:Default:product.html.twig', array(
          'product' =&gt; $product,
          'category' =&gt; $category)
      );
  }
  </code></pre>
  
  <p>If I use this code everything works like it should:</p>
  
  <pre><code>//src\Acme\StoreBundle\Controller\DefaultController.php
  public function showAction($id)
  {
      $product = $this-&gt;getDoctrine()
          -&gt;getRepository('AcmeStoreBundle:Product')
          -&gt;find($id);
  
      if (!$product) {
          throw $this-&gt;createNotFoundException('No product found for id: '.$id);
      }
  
      $category = $product-&gt;getCategory();
  
      return $this-&gt;render('AcmeStoreBundle:Default:product.html.twig', array(
          'product' =&gt; $product,
          'category' =&gt; $category)
      );
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Edit:
  Product Entity:</p>
  
  <pre><code>&lt;?php
  namespace Acme\StoreBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  class Product
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\Column(type="string", length=100)
   */
  protected $name;
  
  /**
   * @ORM\Column(type="decimal", scale=2)
   */
  protected $price;
  
  /**
   * @ORM\Column(type="text")
   */
  protected $description;
  
  /**
   * @ORM\Column(type="text", nullable="true")
   */
  protected $extras;
  
  /**
   * @ORM\ManyToOne(targetEntity="Category", inversedBy="product")
   * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
   */
  protected $category;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set price
   *
   * @param decimal $price
   */
  public function setPrice($price)
  {
      $this-&gt;price = $price;
  }
  
  /**
   * Get price
   *
   * @return decimal 
   */
  public function getPrice()
  {
      return $this-&gt;price;
  }
  
  /**
   * Set description
   *
   * @param text $description
   */
  public function setDescription($description)
  {
      $this-&gt;description = $description;
  }
  
  /**
   * Get description
   *
   * @return text 
   */
  public function getDescription()
  {
      return $this-&gt;description;
  }
  
  /**
   * Set extras
   *
   * @param text $extras
   */
  public function setExtras($extras)
  {
      $this-&gt;extras = $extras;
  }
  
  /**
   * Get extras
   *
   * @return text 
   */
  public function getExtras()
  {
      return $this-&gt;extras;
  }
  
  /**
   * Set category
   *
   * @param Acme\StoreBundle\Entity\Category $category
   */
  public function setCategory(\Acme\StoreBundle\Entity\Category $category)
  {
      $this-&gt;category = $category;
  }
  
  /**
   * Get category
   *
   * @return Acme\StoreBundle\Entity\Category 
   */
  public function getCategory()
  {
      return $this-&gt;category;
  }
  }
  </code></pre>
  
  <p>Category entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\StoreBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Acme\StoreBundle\Entity\Category
   *
   * @ORM\Table()
   * @ORM\Entity
   */
  class Category
  {
  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string $name
   *
   * @ORM\Column(name="name", type="string", length=255)
   */
  private $name;
  
  /**
   * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
   */
  protected $products;
  
  public function __construct()
  {
      $this-&gt;products = new ArrayCollection();
  }
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Add products
   *
   * @param Acme\StoreBundle\Entity\Product $products
   */
  public function addProduct(\Acme\StoreBundle\Entity\Product $products)
  {
      $this-&gt;products[] = $products;
  }
  
  /**
   * Get products
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getProducts()
  {
      return $this-&gt;products;
  }
  }
  </code></pre>