Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 / Repository : Error: __clone method called on non-object

I just started using Symfony and I don't understand why I have this error creating a custom function in a Repository.

My Entity Category.php:

<?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->name;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set parent category
     *
     * @param \HB\PPSBundle\Entity\Category $parent
     * @return Product
     */
    public function setParent(\HB\PPSBundle\Entity\Category $parent)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent category
     *
     * @return \HB\PPSBundle\Entity\Category
     */
    public function getParent()
    {
        return $this->parent;
    }
}

My Repository CategoryRepository.php:

<?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->createQueryBuilder( 'c' );

        $query_builder
            ->where( 'c.parent_id = :parent' )
            ->addOrderBy( 'c.name', 'ASC' )
            ->setParameters( 'parent', $parent );

        echo $query_builder->getDql();

        $query = $query_builder->getQuery();

        //return $query->getArrayResult();

    }

}

And finally the method that call the custom method in CategoryController.php:

public function indexAction()
{

    $em = $this->get('doctrine.orm.entity_manager');
    $repo = $em->getRepository('HBPPSBundle:Category');
    $categories = $repo->findAll();

    $test = $repo->findChildren( 1 );
    echo get_class( $test );

    return $this->render('HBPPSBundle:Categories:index.html.twig', array('categories' => $test));

}

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):

SELECT c FROM HB\PPSBundle\Entity\Category c WHERE c.parent_id = :parent ORDER BY c.name ASC

And finally the error message I get:

FatalErrorException: Error: __clone method called on non-object in /var/www/symfony/2/pps/vendor/doctrine/orm/lib/Doctrine/ORM/QueryBuilder.php line 219

What am I supposed to do get it working?

like image 453
MARTIN Damien Avatar asked Jan 16 '14 16:01

MARTIN Damien


1 Answers

->setParameters( 'parent', $parent ) - not correct.

Try

->setParameter( 'parent', $parent );

like image 169
harentius Avatar answered Nov 14 '22 22:11

harentius