gpt4 book ai didi

php - Symfony 2/存储库 : Error: __clone method called on non-object

转载 作者:可可西里 更新时间:2023-11-01 00:21:23 30 4
gpt4 key购买 nike

我刚开始使用 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

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

最佳答案

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

尝试

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

关于php - Symfony 2/存储库 : Error: __clone method called on non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167869/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com