doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.03k stars 1.27k forks source link

Tree object must have tree metadata at this point #183

Closed juriansluiman closed 12 years ago

juriansluiman commented 12 years ago

I have a similar issue as issue #117, where I get an exception "Tree object class Class\Name must have tree metadata at this point". As far as I know, I register the namespace correctly:

use Doctrine\Common\Annotation\AnnotationRegistry;

AnnotationRegistry::registerAutloadNamespaces(array('Gedmo' => '/path/to/Gedmo/lib'));

I set up the listener:

use Doctrine\Common\EventManager,
    Gedmo\Tree\TreeListener;

$evm  = new EventManager;
$tree = new TreeListener;
$evm->addEventSubscriber($tree);

The eventmanager is passed to the connection, the connection to the entity manager.

My entity:

namespace My\Foo\Model;

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

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

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

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

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

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

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="My\Foo\Model\Page", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
     */
    protected $parent;

    /**
     * @ORM\OneToMany(targetEntity="My\Foo\Model\Page", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    protected $children;
}

What I do to trigger the exception:

// $em instanceof EntityManager
$pages = $em->getRepository('My\Foo\Model\Page')->getRootNodes();

What's wrong and how can I fix this?

l3pp4rd commented 12 years ago

hey, do you use the current version of master branch for extensions? there was an issue some time ago. also annotation namespace should be more specific Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace('Gedmo\\Mapping\\Annotation', 'path/to/gedmo/lib'); note me, if you still have problems

l3pp4rd commented 12 years ago

for instance my silex service config looks like:

<?php
    // load proxy configuration settings
    $config = new Doctrine\ORM\Configuration;
    $config->setProxyDir(__DIR__.'/../tmp');
    $config->setProxyNamespace('Proxy');
    $config->setAutoGenerateProxyClasses(false);
    // load metadata drivers
    $annotationReader = new Doctrine\Common\Annotations\AnnotationReader;
    $refl = new \ReflectionClass('Gedmo\Version');
    $libDir = dirname($refl->getFileName()).'/../../lib';
    Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
        'Gedmo\\Mapping\\Annotation',
        $libDir
    );
    $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
    $annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver($annotationReader, array(
        __DIR__.'/../lib/Gedi/Entity',
        $libDir.'/Gedmo/Translatable/Entity',
        $libDir.'/Gedmo/Loggable/Entity',
        $libDir.'/Gedmo/Tree/Entity',
    ));
    // drivers
    $driverChain->addDriver($annotationDriver, 'Gedi\\Entity');
    $driverChain->addDriver($annotationDriver, 'Gedmo\\Translatable\\Entity');
    $driverChain->addDriver($annotationDriver, 'Gedmo\\Loggable\\Entity');
    $driverChain->addDriver($annotationDriver, 'Gedmo\\Tree\\Entity');
    $config->setMetadataDriverImpl($driverChain);
    // cache
    $config->setMetadataCacheImpl(new Doctrine\Common\Cache\ArrayCache);
    $config->setQueryCacheImpl(new Doctrine\Common\Cache\ArrayCache);
    // event manager
    $app['sluggable'] = new Gedmo\Sluggable\SluggableListener;
    $app['db.evm']->addEventSubscriber($app['sluggable']);
    $app['db.evm']->addEventSubscriber(new Gedmo\Tree\TreeListener);
    $app['db.evm']->addEventSubscriber(new Gedmo\Loggable\LoggableListener);
    $app['db.evm']->addEventSubscriber(new Gedmo\Timestampable\TimestampableListener);
    $translatable = new Gedmo\Translatable\TranslationListener;
    $translatable->setTranslatableLocale('en');
    $app['db.evm']->addEventSubscriber($translatable);
    // create entity manager
    return Doctrine\ORM\EntityManager::create($app['db'], $config);
l3pp4rd commented 12 years ago

this might happen if you have doctrine common 2.2 version, I would recommend to use 2.1 still

juriansluiman commented 12 years ago

@l3pp4rd Thanks for all the updates. I think I missed the Gedmo\Tree\Entity annotation driver in the driver chain. Also I use a Zend Framework 2 module which ships with Doctrine 2.2 by default. I will have a look to fix both issues and get back with my results :)

juriansluiman commented 12 years ago

I think the issue is fixed to return to the Doctrine 2.1.2 version. I will notify the maintainer of the zf2 module to include the 2.1.2 version with his module. Thanks!

l3pp4rd commented 12 years ago

I currently trying to adapt the mapping to doctrine common 2.2, but ODM is also late for changes and does not work with latest components