use import as ORM without symfony

Showing 1-7 of 7 messages
use import as ORM without symfony Matteo Giachino 7/8/12 3:46 PM
Am I missing something to make

use Doctrine\ORM\Mapping as ORM;

work?

I'm using doctrine in a test suite, and I'm building manually all the configurations.

Here is my code:

                $doctrineDir = realpath(__DIR__.'/../vendor/doctrine');
        $classLoader = new ClassLoader('Doctrine\Common', $doctrineDir . '/common/lib');
        $classLoader->register();

        $classLoader = new ClassLoader('Doctrine\DBAL', $doctrineDir . '/dbal/lib');
        $classLoader->register();

        $classLoader = new ClassLoader('Doctrine\ORM', $doctrineDir . '/orm/lib');
        $classLoader->register();

        $cache = new ArrayCache();
        $config = new Configuration;
        $config->setMetadataCacheImpl($cache);
        $driverImpl = $config->newDefaultAnnotationDriver(__DIR__.'/Playground/Entity');
        $config->setMetadataDriverImpl($driverImpl);
        $config->setQueryCacheImpl($cache);
        $config->setProxyDir(__DIR__.'/Playground/Proxies');
        $config->setProxyNamespace('Cypress\TranslationBundle\Tests\Playground\Proxies');

        $config->setAutoGenerateProxyClasses(true);

        $connectionOptions = array(
            'driver' => 'pdo_sqlite',
            'path' => $this->dbFile
        );


Creating the schema with SchemaTool class works fine with an entity and "normal" annotations like @Entity, @Column etc..

If I add
        use Doctrine\ORM\Mapping as ORM;
and use annotations like ORM\Entity or ORM\Column gives:

Doctrine\ORM\Mapping\MappingException: Class MyNamespace\Entity\Book is not a valid entity or mapped super class

Many thanks.
Regards.
Re: [doctrine-user] use import as ORM without symfony Marco Pivetta 7/8/12 4:26 PM
That is because you are using `Doctrine\ORM\Configuration#newDefaultAnnotationDriver`.

Instead, use

$config->setMetadataDriverImpl(
    new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
        $paths,
        new Doctrine\Common\Annotations\CachedReader(
            new Doctrine\Common\Annotations\AnnotationReader(),
            new Doctrine\Common\Cache\ApcCache() // cache you want
        )
    )
);

(all the clutter you see is because you have to wrap an AnnotationReader in a CachedReader, otherwise parsing annotations each time is too slow ;)
Marco Pivetta

http://twitter.com/Ocramius     

http://marco-pivetta.com    




--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/doctrine-user/-/f5NadOrRJx0J.
To post to this group, send email to doctri...@googlegroups.com.
To unsubscribe from this group, send email to doctrine-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/doctrine-user?hl=en.

Re: [doctrine-user] use import as ORM without symfony Matteo Giachino 7/8/12 11:36 PM
many thanks for the quick respose, now I have:


        $doctrineDir = realpath(__DIR__.'/../vendor/doctrine');
        $classLoader = new ClassLoader('Doctrine\Common', $doctrineDir . '/common/lib');
        $classLoader->register();

        $classLoader = new ClassLoader('Doctrine\DBAL', $doctrineDir . '/dbal/lib');
        $classLoader->register();

        $classLoader = new ClassLoader('Doctrine\ORM', $doctrineDir . '/orm/lib');
        $classLoader->register();

        $cache = new ArrayCache();
        $config = new Configuration;
        $config->setMetadataDriverImpl(
            new AnnotationDriver(
                new CachedReader(
                    new AnnotationReader(),
                    $cache
                ),
                array(__DIR__.'/Playground/Entity')
            )
        );
        $config->setMetadataCacheImpl($cache);

        $config->setQueryCacheImpl($cache);
        $config->setProxyDir(__DIR__.'/Playground/Proxies');
        $config->setProxyNamespace('Cypress\TranslationBundle\Tests\Playground\Proxies');

        $config->setAutoGenerateProxyClasses(true);

        $connectionOptions = array(
            'driver' => 'pdo_sqlite',
            'path' => $this->dbFile
        );

        $this->em = EntityManager::create($connectionOptions, $config);
        return $this->em;

The annotation driver seems to work...but I got this error:

Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class Cypress\TranslationBundle\Tests\Playground\Entity\Book does not exist, or could not be auto-loaded.

Any ideas?
Matteo.


Il giorno lunedì 9 luglio 2012 01:26:21 UTC+2, Marco Pivetta ha scritto:
Re: [doctrine-user] use import as ORM without symfony szjani 7/8/12 11:49 PM
Hi,

You use Doctrine\ORM\Mapping as ORM; in your entities so you should use @ORM instead of @Doctrine\ORM within annotations.

2012. július 9., hétfő 8:36:36 UTC+2 időpontban Matteo G a következőt írta:
Re: [doctrine-user] use import as ORM without symfony Benjamin Eberlei 7/9/12 12:47 AM
you have to call \Doctrine\Common\Annotations\AnnotationRegistry::registerLoad($classLoader); of the ORM class loader.

To view this discussion on the web visit https://groups.google.com/d/msg/doctrine-user/-/m6pPmkhI4fYJ.

To post to this group, send email to doctri...@googlegroups.com.
To unsubscribe from this group, send email to doctrine-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/doctrine-user?hl=en.

Re: [doctrine-user] use import as ORM without symfony Benjamin Eberlei 7/9/12 12:48 AM
Hm no thats wrong, AnnotationRegistry::registerNamespace("Doctrine\ORM", "/orm/lib");

In 2.3 we will change this to have newDefaultAnnotationDriver() also create this kind of annotation loader via a flag.
Re: [doctrine-user] use import as ORM without symfony Matteo Giachino 7/9/12 11:39 AM
YES!
Many thanks you all...

The correct solution in the end is to call:

AnnotationRegistry::registerAutoloadNamespace("Doctrine\ORM", '/path/to/doctrine/orm/lib');

Regards.

Il giorno lunedì 9 luglio 2012 09:48:23 UTC+2, Benjamin Eberlei ha scritto: