脚本宝典收集整理的这篇文章主要介绍了php – Symfony 2 / Repository:错误:在非对象上调用__clone方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的实体Category.PHP:
@H_777_9@<?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
我该怎么办才能让它发挥作用?
尝试
– > setParameter(‘parent’,$parent);
以上是脚本宝典为你收集整理的php – Symfony 2 / Repository:错误:在非对象上调用__clone方法全部内容,希望文章能够帮你解决php – Symfony 2 / Repository:错误:在非对象上调用__clone方法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。