Symfony 2 / 存储库 : 错误: __clone在非对象上调用的方法



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

我的实体Category.php

<?php
namespace HBPPSBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Category
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="HBPPSBundleEntityCategoryRepository")
 */
class Category
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255)
     */
    private $name;
    /**
     * @ORMOneToOne(targetEntity="HBPPSBundleEntityCategory", mappedBy="name", cascade={"persist"})
     * @ORMJoinColumn(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 HBPPSBundleEntityCategory $parent
     * @return Product
     */
    public function setParent(HBPPSBundleEntityCategory $parent)
    {
        $this->parent = $parent;
        return $this;
    }
    /**
     * Get parent category
     *
     * @return HBPPSBundleEntityCategory
     */
    public function getParent()
    {
        return $this->parent;
    }
}

我的存储库CategoryRepository.php

<?php
namespace HBPPSBundleEntity;
use DoctrineORMEntityRepository;
/**
 * 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 (HBPPSBundleEntityCategory::$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 HBPPSBundleEntityCategory 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

我应该怎么做才能让它工作?

-> setParameters( 'parent', $parent ) - 不正确。

尝试

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

我终于找到了如何让它工作:

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

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

public function findChildren( $parent )
{
    $em = $this->getEntityManager();
    $query = $em->createQuery(
        'SELECT c
        FROM HBPPSBundleEntityCategory c
        WHERE c.parent = :parent
        ORDER BY c.name ASC'
    )->setParameter( 'parent', $parent );
    $categories = $query->getResult();
    return $categories;
}

必须将setParameters设置为数组:

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

相关内容

  • 没有找到相关文章

最新更新