加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:50:32 所属栏目:PHP教程 来源:网络整理
导读:我刚开始使用Symfony,我不明白为什么我在存储库中创建自定义函数时出现此错误. 我的实体Category.php: ?phpnamespace HBPPSBundleEntity;use DoctrineORMMapping as ORM;/** * Category * * @ORMTable() * @ORMEntity(repositoryClass="HBPPSBundle
我刚开始使用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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读