0

I am getting error:

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

I am using:

Symfony v3.0.6;
Doctrine v2.5.4
StofDoctrineExtensionsBundle

in order to manage Tree structure.

In order to configure tree repository using traits I do as in documentation. And I get Error that is showed in the beginning of the question. Note that if I configure tree repository by extending Abstract repository as said in documentation i have different error

How can i provide "instance of AppBundle\Repository\ClassMetadata" to argument two?

My tree repository

// 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);
    }
}

My category entity

// 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;
    }
}

Please advise. Thank you for your time and knowledge.

Community
  • 1
  • 1
Rikijs
  • 728
  • 1
  • 12
  • 48

1 Answers1

1

Seems you miss to import the namespace of the class

so add this statement in the use lists of the class CategoryRepository:

use Doctrine\ORM\Mapping\ClassMetadata;

So try this:

AppBundle\Repository\CategoryRepository.php

namespace AppBundle\Repository;

use Gedmo\Tree\Traits\Repository\ORM\NestedTreeRepositoryTrait;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;  // <-- ADD THIS

class CategoryRepository extends EntityRepository
{
    use NestedTreeRepositoryTrait;

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

        $this->initializeTreeRepository($em, $class);
    }
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Thanks Matteo, no more argument error - it now shows different error though. One descripted [here on StackOverflow](http://stackoverflow.com/questions/37217961/node-is-not-related-to-this-repository-500-internal-server-error-invalidargume) – Rikijs May 17 '16 at 17:06
  • It also works with `use Doctrine\Common\Persistence\Mapping\ClassMetadata;` do not know which way is better: Your suggestion or this code or it does not matter!? – Rikijs May 17 '16 at 17:10
  • hi @Rikijs in PHP there isn't much difference. BTW you should use the same type declared in the constructor you are overriding. Of course, you specify an interface so is usually better than a concrete implementation class. – Matteo May 17 '16 at 17:27