R
R
Roman-G2015-11-08 12:59:56
Doctrine ORM
Roman-G, 2015-11-08 12:59:56

How to use Tree in Doctrine2 behavioral extensions?

I can't get it to work with the Doctrine2 behavioral extensions library .
Namely, the Tree functionality is needed.
Error:
Fatal error: Uncaught exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@Entity" in class Tree was never imported. Did you maybe forget to add a "use" statement for this annotation?' in C:\OpenServer\domains\doctrine\vendor\doctrine\annotations\lib\Doctrine\Common\Annotations\AnnotationException.php on line 54
Bootstrap.php

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require_once "vendor/autoload.php";

$entityPath = array('src');

$isDevMode = true;

$config = Setup::createAnnotationMetadataConfiguration($entityPath, $isDevMode);

$databaseParameters = array('driver' => 'pdo_mysql', 
                            'user' => 'root', 
                            'password' => '', 
                            'dbname' => 'doctrine');

$entityManager = EntityManager::create($databaseParameters, $config);

/src/Tree.php
<?php

/**
 * @Entity(repositoryClass="TreeRepository") 
 * @Table(name="Tree")
 */
class Tree {
  
  /**
   * @Id @GeneratedValue @Column(type="integer")
   * 
   * @var int
   */
  private $id;
  
  public function getId() {
    return $this->id;
  }
}

/src/TreeRepository.php
<?php

use Gedmo\Tree\Entity\Repository\NestedTreeRepository;

class TreeRepository extends NestedTreeRepository
{
}

tree.php
require_once 'bootstrap.php';

$treeListener = new Gedmo\Tree\TreeListener();

$entityManager->getEventManager()->addEventSubscriber($treeListener);

$repository = $entityManager->getRepository('Tree');

$subNode = $repository->findOneByTitle('root');

$path = $repository->getPath($subNode);

What is the error? I spent a lot of time on the solution, and did not figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Latyshev, 2015-11-08
@Roman-G

You need to add use and required fields marked as TreeLeft, TreeRight, TreeLevel, etc.

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="Tree")
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Tree
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

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

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

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

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

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

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question