title: Symfony 2 / Repository : Error: __clone method called on non-object
body: <p>I just started using Symfony and I don't understand why I have this error creating a custom function in a Repository.</p>
  
  <p>My Entity <code>Category.php</code>:</p>
  
  <pre><code>&lt;?php
  
  namespace HB\PPSBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Category
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="HB\PPSBundle\Entity\CategoryRepository")
   */
  class Category
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\OneToOne(targetEntity="HB\PPSBundle\Entity\Category", mappedBy="name", cascade={"persist"})
       * @ORM\JoinColumn(nullable=true)
       */
      private $parent;
  
  
      public function __toString()
      {
          return $this-&gt;name;
      }
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Category
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set parent category
       *
       * @param \HB\PPSBundle\Entity\Category $parent
       * @return Product
       */
      public function setParent(\HB\PPSBundle\Entity\Category $parent)
      {
          $this-&gt;parent = $parent;
  
          return $this;
      }
  
      /**
       * Get parent category
       *
       * @return \HB\PPSBundle\Entity\Category
       */
      public function getParent()
      {
          return $this-&gt;parent;
      }
  }
  </code></pre>
  
  <p>My Repository <code>CategoryRepository.php</code>:</p>
  
  <pre><code>&lt;?php
  
  namespace HB\PPSBundle\Entity;
  
  use Doctrine\ORM\EntityRepository;
  
  /**
   * CategoryRepository
   *
   * This class was generated by the Doctrine ORM. Add your own custom
   * repository methods below.
   */
  class CategoryRepository extends EntityRepository
  {
  
      /**
       * Get all the children of a given category.
       *
       * @var integer The parent ID (HB\PPSBundle\Entity\Category::$id)
       */
      public function findChildren( $parent )
      {
  
          $query_builder = $this-&gt;createQueryBuilder( 'c' );
  
          $query_builder
              -&gt;where( 'c.parent_id = :parent' )
              -&gt;addOrderBy( 'c.name', 'ASC' )
              -&gt;setParameters( 'parent', $parent );
  
          echo $query_builder-&gt;getDql();
  
          $query = $query_builder-&gt;getQuery();
  
          //return $query-&gt;getArrayResult();
  
      }
  
  }
  </code></pre>
  
  <p>And finally the method that call the custom method in <code>CategoryController.php</code>:</p>
  
  <pre><code>public function indexAction()
  {
  
      $em = $this-&gt;get('doctrine.orm.entity_manager');
      $repo = $em-&gt;getRepository('HBPPSBundle:Category');
      $categories = $repo-&gt;findAll();
  
      $test = $repo-&gt;findChildren( 1 );
      echo get_class( $test );
  
      return $this-&gt;render('HBPPSBundle:Categories:index.html.twig', array('categories' =&gt; $test));
  
  }
  </code></pre>
  
  <p>When I run this, I can see the generated DQL (I don't know what I'm supposed to have in this, but it looks like what I found on other questions):</p>
  
  <pre><code>SELECT c FROM HB\PPSBundle\Entity\Category c WHERE c.parent_id = :parent ORDER BY c.name ASC
  </code></pre>
  
  <p>And finally the error message I get:</p>
  
  <pre><code>FatalErrorException: Error: __clone method called on non-object in /var/www/symfony/2/pps/vendor/doctrine/orm/lib/Doctrine/ORM/QueryBuilder.php line 219
  </code></pre>
  
  <p>What am I supposed to do get it working?</p>