Symfony 2 / Repository:错误:在非对象上调用__clone方法

时间:2014-01-16 16:51:01

标签: php symfony doctrine-orm repository

我刚刚开始使用Symfony,我不明白为什么在存储库中创建自定义函数时出现此错误。

我的实体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;
    }
}

我的存储库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();

    }

}

最后是在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));

}

当我运行它时,我可以看到生成的DQL(我不知道我应该拥有什么,但它看起来像我在其他问题上找到的那样):

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

最后我收到错误消息:

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

我该怎么办才能让它发挥作用?

3 个答案:

答案 0 :(得分:7)

- &gt; setParameters( 'parent', $parent ) - 不正确。

尝试

- &GT; setParameter( 'parent', $parent );

答案 1 :(得分:0)

我终于找到了如何让它发挥作用:

由于错误来自getQuery() QueryBuilder,我直接进行了查询。

所以存储库中的代码现在是:

public function findChildren( $parent )
{

    $em = $this->getEntityManager();
    $query = $em->createQuery(
        'SELECT c
        FROM HB\PPSBundle\Entity\Category c
        WHERE c.parent = :parent
        ORDER BY c.name ASC'
    )->setParameter( 'parent', $parent );

    $categories = $query->getResult();

    return $categories;

}

答案 2 :(得分:0)

您必须将setParameters设置为数组:

 ->setParameters(array( 'parent' => $capacita))