title: Symfony2: How to convert 'Doctrine->getRepository->find' from Entity to Array?
body: <p>I want to return an \Symfony\Component\HttpFoundation\JsonResponse with the record information, but I need to pass it as array.</p>
  
  <p>Currently I do: 
  </p>
  
  <pre><code>$repository = $this-&gt;getDoctrine()-&gt;getRepository('XxxYyyZzzBundle:Products');
  $product = $repositorio-&gt;findOneByBarCode($value);
  </code></pre>
  
  <p>But now $product is an Entity containing all what I want, but as Objects. How could I convert them to arrays?</p>
  
  <p>I read somewhere that I need to use "Doctrine\ORM\Query::HYDRATE_ARRAY" but seems 'findOneBy' magic filter does not accept such parameter.</p>
  
  <pre class="lang-php prettyprint-override"><code>*
  * @return object|null The entity instance or NULL if the entity can not be found.
  */
  public function findOneBy(array $criteria, array $orderBy = null)
  </code></pre>
  
  <hr>
  
  <p>Well thanks to dbrumann I got it working, just want to add it the full working example.</p>
  
  <p>Also seems that ->from() requires 2 parameters.</p>
  
  <pre class="lang-php prettyprint-override"><code>    $em = $this-&gt;getDoctrine()-&gt;getManager();  
      $query = $em-&gt;createQueryBuilder()  
              -&gt;select('p')  
              -&gt;from('XxxYyyZzzBundle:Products','p')  
              -&gt;where('p.BarCode = :barcode')  
              -&gt;setParameter('barcode', $value)  
              -&gt;getQuery()  
              ;  
  
      $data = $query-&gt;getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
  </code></pre>