我收到错误:
Type error: Argument 1 passed to AppBundle\Repository\CategoryRepository::__construct()
must be an instance of AppBundle\Repository\EntityManager,
instance of Doctrine\ORM\EntityManager given,
called in C:\_DEV_SF3_\fs3_tree\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php on line 68
我正在使用:
Symfony v3.0.6;
Doctrine v2.5.4
StofDoctrineExtensionsBundle
以管理树结构。
为了使用我在documentation中执行的特征配置树存储库。我得到的错误是在问题的开头显示的。 请注意,如果我通过扩展Abstract存储库来配置树存储库,如documentation中所述different error
如何向参数1提供“AppBundle \ Repository \ EntityManager实例”?
我的树库
// AppBundle\Repository\CategoryRepository.php
<?php
namespace AppBundle\Repository;
use Gedmo\Tree\Traits\Repository\ORM\NestedTreeRepositoryTrait;
use Doctrine\ORM\EntityRepository;
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;
}
}
请指教。感谢您的时间和知识。
答案 0 :(得分:0)
您在use
课程中缺少Doctrine EntityManager
课程的CategoryRepository
声明:
namespace AppBundle\Repository;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
class CategoryRepository extends EntityRepository
{
use NestedTreeRepositoryTrait;
public function __construct(EntityManager $em, ClassMetadata $class)
{
// ...
}
}