Doctrine 2 ORM

Hi Everyone,

Just a heads up that I’ve released my first implementation of Doctrine 2 ORM and Yii.

Feedback is appreciated.

Get the extension here

Looks good. I had implemented my own Doctrine 2 extension, but yours looks more complete so I will switch to using it instead.

In terms of implementation, the only suggestion I have is using


\Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli);

to add all the console commands rather than adding them individually.

Also, in your sample configuration you set a value for ‘entityPath’ that is never used (there is no corresponding setter method in the Configuration class). I am guessing that this was the precursor to ‘mappingPaths’ which is where this value should go.

Thanks for your comments, I’ll take a look at the sample config. It may be that I had a change of mind during initial development of option names.

Thanks wk.

I’m in first stage of choosing a ORM layer for our new project. I choose MongoDB and Yii. But as author of YiiMongoDBSuite said that his extensions is not best for MongoDB, so we are estimating Doctrine2. I’m new to Doctrine (but I’ve experienced with Java’s Hibernate) so I would like to ask that could we use the transparent feature of Doctrine MongoDB extensions with your extension?

@yii guys :), I hear that the next Yii 2.0 version will have the same feature as Doctrine does that transparent to DB layer no matter MySQL or NoSQL or MongoDB :). Could I wait to that?

Regards,

Khanh Tran

Hi there,

This plugin was written for the for the ORM project only and won’t work with the MongoDB ODM library but it may be something I’ll consider adding if there is enough interest in its implementation.

wk

Dear all,

I just changed some code to add ODM to Wildkat extension. Thanks wk for a nice extensions, it help me to understand about Yii classloader and PHP 5.3 classloader :)

** Note that: you should get new YamlDriver from Symfony, I could not make Annotation work, but I think YAML is good because we can change ODM <-> ORM versa.

Here is the code:




public function getMongo() {

   		/**

        * MongoDB handler...

        */

        $configD = new MongoDBConfiguration();

        $configD->setProxyDir('/tmp/doctrine-mongo');

        $configD->setProxyNamespace('Proxies');

 

        $configD->setHydratorDir('/tmp/doctrine-mongo/cache');

		$configD->setHydratorNamespace('Hydrators');

        

		$options = array();

        $options['mappingPaths'] = 'ext.Wildkat.YiiExt.DoctrineOrm.mapping.mongo';

        $options['mappingDriver'] = 'YamlDriver';

        $driver = $this->_getMongoMappingDriver($options);

        $configD->setMetadataDriverImpl($driver);

 

        try { 

        	$this->dm = DocumentManager::create(new MongoConnection("mongodb://localhost"), $configD);

        } catch (Exception $e) { 

        	var_dump($e->getMessage()); 

        }

        return $this->dm;

    }


/**

     * Take the configuration and return a mapping driver

     *

     * @param array &$config the driver options

     *

     * @return Doctrine\ORM\Mapping\Driver\Driver

     */

    private function _getMongoMappingDriver(array & $config)

    {

        $drivers = array(

            'XmlDriver'  => 'Doctrine\ORM\Mapping\Driver\XmlDriver',

            'YamlDriver' => 'Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver',

        );


        if (is_array($config['mappingPaths']) === true) {

            foreach ($config['mappingPaths'] as $index => $path) {

                $config['mappingPaths'][$index] = \Yii::getPathOfAlias($path);

            }

        } else {

            $config['mappingPaths'] = \Yii::getPathOfAlias($config['mappingPaths']);

        }


        // set default annotation driver

        if (array_key_exists($config['mappingDriver'], $drivers) === false

            || $config['mappingDriver'] === 'AnnotationDriver'

        ) {

            $reader = new AnnotationReader();

            $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');

            $driver = new AnnotationDriver($reader, $config['mappingPaths']);

            unset($config['mappingDriver']);

            unset($config['mappingPaths']);

            return $driver;

        }


        $mappingClass = $drivers[$config['mappingDriver']];

        $driver       = new $mappingClass($config['mappingPaths']);


        if (isset($config['mappingDriverOptions']) === true) {

            foreach ($config['mappingDriverOptions'] as $key => $value) {

                $method = 'set' . ucfirst($key);

                if (method_exists($driver, $method) === true) {

                    $driver->{$method}($value);

                }

            }


            unset($config['mappingDriverOptions']);

        }


        unset($config['mappingDriver']);

        unset($config['mappingDriverPaths']);


        return $driver;


    }//end _getMappingDriver()



Hi khanhtran,

Thanks for taking the time to look at improvements to my initial extensions. I’ll take a look at your code to see if it can be incorporated. At first glance though, I would make changes in how it pull ODM configuration to allow for multiple configurations like the ORM does.

wk

Hi hi…

How its going the project?? I do not see any change from May 13…

I think this it’s a great extension, but Im so losing in the code :P

Anyway, where I must put my Entities Classes?

I must include some code before declare the Entitie Class?




/**

* Something here?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?

*/

class MyEntity{


}

Thanks

@yyeshua

Your entity classes need to be somewhere on your current include path. You can also put them inside your models directory, see the example here: http://code.google.com/p/yii-doctrine2/source/browse/trunk/protected/models/entity/User.php

Oh man, many thanks :)

Hey… I was trying but I have some problem. I follow the config files, i was download the Doctrine ORM, i was move the Command… well, the case is when I execiute:


php yiic doctrineorm orm:schema-tool create

I get "No Metadata classes to process".

Some idea??

When I execute: php yiic.php doctrineorm orm:schema-tool:create

I get this error:

[Doctrine\ORM\Mapping\MappingException]

File mapping drivers must have a valid directory path, however the given path [/opt/project/protected/extensions/Wildkat/YiiExt/DoctrineOrm/mapping] seems to be incorrect!

I’ve take a look at the path and the mapping directory doesn’t exists.

If I create it I get the message: "No Metadata classes to process."

The mapping path came from the config file:

‘entityManager’ => array(

            'default' =&gt; array(


                'connection' =&gt; 'default',


                'metadataCache' =&gt; 'default',


                'queryCache' =&gt; 'default',


                'entityPath' =&gt; 'application.models.entity',


                'mappingDriver' =&gt; 'AnnotationDriver',


                'mappingPaths' =&gt; array(


                    'ext.Wildkat.YiiExt.DoctrineOrm.mapping'


                ),


                'proxyDir' =&gt; 'application.data',


                'proxyNamespace' =&gt; 'Proxy',


            ),


        ),

I didn’t find any documentation about the config so I don’t know what to do with the mappingPaths. should it be the path where I’ve create my Annotation classes? I’ve try without any luck.

Can someone help me about this, or can provide me a config documentation?

thanks

For those who get the same issue, I’ve find that it’s the mappingPaths. I’ve change the mappingPaths to the path where my entities annotation classes are:

‘entityManager’ => array(

‘default’ => array(

‘connection’ => ‘default’,

‘metadataCache’ => ‘default’,

‘queryCache’ => ‘default’,

‘entityPath’ => ‘application.models.Entity’,

‘mappingDriver’ => ‘AnnotationDriver’,

‘mappingPaths’ => array(

‘application.models.Entities’

),

‘proxyDir’ => ‘application.data’,

‘proxyNamespace’ => ‘Proxy’,

),

),

now the command line: php yiic doctrineorm orm:schema-tool:create --dump-sql gor futher but get a problem loading the classes:

ATTENTION: This operation should not be executed in an production enviroment.

Creating database schema…

PHP Error[2]: include(test.php): failed to open stream: No such file or directory

in file /opt/fw/yii-1.1.8/framework/YiiBase.php at line 421

#0 /opt/fw/yii-1.1.8/framework/YiiBase.php(421): autoload()

#1 unknown(0): autoload()

#2 unknown(0): spl_autoload_call()

To fix the class loading issue, do not use namespace into the annotation classes cause the yii autoloader doesn’t work using them and will not find the libraries.

I will also mention that the vendor/Symfony directory need to get a copy of the Doctrine/Symfony/Component directory.

hope it help someone.

Can’t get it working with annotations. How to configure ?

DoctrineContainer line 391:

$reader->setDefaultAnnotationNamespace(‘Doctrine\ORM\Mapping\\’);

Fatal error: Call to undefined method Doctrine\Common\Annotations\AnnotationReader::setDefaultAnnotationNamespace() in […]\DoctrineContainer.php on line 391

Without that line I get this:

Doctrine\Common\Annotations\AnnotationException

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class Bla does not exist, or could not be auto-loaded.

Hi, did you get any solution for this issue?? I’m also facing the same problem.

Hi wk

Is there any chance to find compatible RBAC module with doctrine 2 ?

thx a lot