使用Symony3和stofDoctrineExtensionsBundle配置具有特征的Gedmo树repoository时出现参数2错误

时间:2016-05-17 14:14:13

标签: tree symfony doctrine-extensions stofdoctrineextensions

我收到错误:

Type error: Argument 2 passed to AppBundle\Repository\CategoryRepository::__construct()
must be an instance of AppBundle\Repository\ClassMetadata, 
instance of Doctrine\ORM\Mapping\ClassMetadata given, 
called in C:\_DEV_SF3_\fs3_tree\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRep‌​ositoryFactory.php on line 68
500 Internal Server Error - FatalThrowableError

我正在使用:

Symfony v3.0.6;
Doctrine v2.5.4
StofDoctrineExtensionsBundle

以管理树结构。

为了使用我在documentation中执行的特征配置树存储库。我得到的错误是在问题的开头显示的。 请注意,如果我通过扩展Abstract存储库来配置树存储库,如documentation中所述different error

我如何提供" AppBundle \ Repository \ ClassMetadata"的实例论证两个?

我的树库

// AppBundle\Repository\CategoryRepository.php
<?php

namespace AppBundle\Repository;

use Gedmo\Tree\Traits\Repository\ORM\NestedTreeRepositoryTrait;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;

class CategoryRepository extends EntityRepository
{
    use NestedTreeRepositoryTrait;

    public function __construct(EntityManager $em, ClassMetadata $class)
    {
        parent::__construct($em, $class);

        $this->initializeTreeRepository($em, $class);
    }
}

我的类别实体

// AppBundle\Entity\Category.php
<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="categories")
 * use repository for handy tree functions
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column(length=64)
     */
    private $title;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getRoot()
    {
        return $this->root;
    }

    public function setParent(Category $parent = null)
    {
        $this->parent = $parent;
    }

    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set lft
     *
     * @param integer $lft
     *
     * @return Category
     */
    public function setLft($lft)
    {
        $this->lft = $lft;

        return $this;
    }

    /**
     * Get lft
     *
     * @return integer
     */
    public function getLft()
    {
        return $this->lft;
    }

    /**
     * Set lvl
     *
     * @param integer $lvl
     *
     * @return Category
     */
    public function setLvl($lvl)
    {
        $this->lvl = $lvl;

        return $this;
    }

    /**
     * Get lvl
     *
     * @return integer
     */
    public function getLvl()
    {
        return $this->lvl;
    }

    /**
     * Set rgt
     *
     * @param integer $rgt
     *
     * @return Category
     */
    public function setRgt($rgt)
    {
        $this->rgt = $rgt;

        return $this;
    }

    /**
     * Get rgt
     *
     * @return integer
     */
    public function getRgt()
    {
        return $this->rgt;
    }

    /**
     * Set root
     *
     * @param \AppBundle\Entity\Category $root
     *
     * @return Category
     */
    public function setRoot(\AppBundle\Entity\Category $root = null)
    {
        $this->root = $root;

        return $this;
    }

    /**
     * Add child
     *
     * @param \AppBundle\Entity\Category $child
     *
     * @return Category
     */
    public function addChild(\AppBundle\Entity\Category $child)
    {
        $this->children[] = $child;

        return $this;
    }

    /**
     * Remove child
     *
     * @param \AppBundle\Entity\Category $child
     */
    public function removeChild(\AppBundle\Entity\Category $child)
    {
        $this->children->removeElement($child);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChildren()
    {
        return $this->children;
    }
}

请指教。感谢您的时间和知识。

1 个答案:

答案 0 :(得分:1)

似乎你错过了导入类的命名空间

所以在类א 2 1 ב 的{​​{1}}列表中添加此语句:

use

所以试试这个:

的appbundle \库\ CategoryRepository.php     

CategoryRepository