Welcome Guest, Not a member yet? Register   Sign In
Doctrine ODM for MongoDB with Codeigniter 2.0 (Code now avaliable)
#1

[eluser]Abu eldahab[/eluser]
Hello,

Is there any library allow us to use Doctrine ODM for MongoDB with Codeigniter 2.0 ?

I mean before i was using doctrine_pi.php as a plugin to load Doctrine ORM

now no plugins in Codeigniter 2.0 and also i need to use Doctrine ODM for MongoDB

please help

Regards
#2

[eluser]Abu eldahab[/eluser]
Hello, I searched on the web and found it

this is the way

1- create php file inside libraries folder called Doctrine.php and put this code inside it
Code:
<?php

use Doctrine\Common\ClassLoader,
    Doctrine\Common\Annotations\AnnotationReader,
    Doctrine\ODM\MongoDB\DocumentManager,
    Doctrine\ODM\MongoDB\Mongo,
    Doctrine\ODM\MongoDB\Configuration,
    Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;


class Doctrine {

  
    public $dm = null;

    public function __construct()
    {
    
        require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';

        $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
        $doctrineClassLoader->register();
        $entitiesClassLoader = new ClassLoader('DW', APPPATH.'models');
        $entitiesClassLoader->register();
        $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
        $proxiesClassLoader->register();

      
        /**
        * MongoDB handler...
        */
        $configD = new Configuration();
        $configD->setProxyDir('<<PATH_TO_MONGODB_PROXIES>>');
        $configD->setProxyNamespace('Proxies');

        $readerD = new AnnotationReader();
        $readerD->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
        $configD->setMetadataDriverImpl(new AnnotationDriver($readerD, APPPATH.'models/<<NAMESPACE>>'));

        try { $this->dm = DocumentManager::create(new Mongo("<<MONGODB_SERVER>>"), $configD); } catch (Exception $e) { var_dump($e->getMessage()); }

    }
}



2- create folder inside Codeigniter libraries Called Doctrine and download doctrine odm and common and put them inside it

Doctrine Common from here

http://www.doctrine-project.org/projects/common

and ODM from here

http://www.doctrine-project.org/projects/mongodb_odm


3- your files should look like this

-application
---libraries
Doctrine
---ODM
---common
Doctrine.php
index.html


replace vars with your server configuration for example "<<MONGODB_SERVER>>" with "localhost:27017"

for more info read this

http://www.doctrine-project.org/projects...duction/en

Regards

Ahmed
#3

[eluser]debonair[/eluser]
Hi Ahmed,

Could you provide a simple tutorial to show how to use Doctrine, a simple CRUD application,

thanks,

Debonair
#4

[eluser]junkwax[/eluser]
If you have some errors with code Abu eldahab provided, like Configuration.php include error or Mongo.php not found, read this thread:

http://groups.google.com/group/doctrine-...5b16880801
#5

[eluser]nsouto[/eluser]
Hello,

I've been going crazy trying to get this to work, I haven't been able to get it to work by itself or the way I wanted it, with ORM and ODM working together.

I followed the link suggested by junkwax, but I can't seem to get it to work.

I keep getting:

Code:
Message: require(application/third_party/doctrine-orm/Doctrine/MongoDB/Configuration.php) [function.require]: failed to open stream: No such file or directory

Or if I comment out this on Doctrine/ODM/MongoDB/Configuration.php file:

Code:
class Configuration /* extends \Doctrine\MongoDB\Configuration */

Then I get:

Code:
Message: Argument 1 passed to Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::__construct() must be an instance of Doctrine\Common\Annotations\Reader, instance of Doctrine\Common\Annotations\AnnotationReader given, called in /Users/nsouto/Sites/temp/application/libraries/Doctrine.php on line 40 and defined

I tried it with MongoDB ODM by itself or mixing it with ORM I always get the same problem.

I've searched around for a solution to this but I can't seem to find one.

If anyone has a working sample with ODM. I'd appreciate some help.

Thanks
#6

[eluser]nsouto[/eluser]
Here is the Doctrine.php file I'm using to get this working.

I've been playing around with it so much I don't even know where I started anymore:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
        Doctrine\Common\Annotations\AnnotationReader,
        Doctrine\ORM\Mapping\Driver\AnnotationDriver,
    Doctrine\DBAL\Logging\EchoSqlLogger,
        Doctrine\DBAL\Event\Listeners\MysqlSessionInit,
        Doctrine\ORM\Tools\SchemaTool,
        Doctrine\Common\EventManager,
        Gedmo\Timestampable\TimestampableListener,
        Gedmo\Sluggable\SluggableListener,
        Gedmo\Tree\TreeListener,

    Doctrine\ODM\MongoDB\Configuration as MongoDBConfiguration,
    Doctrine\ODM\MongoDB\DocumentManager,
/*     Doctrine\ODM\MongoDB\Mongo, */
    Doctrine\MongoDB\Connection,
    Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver as MongoAnnotationDriver,
    Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver;

/**
* CodeIgniter Doctrine Library
* @author            Rubén Sarrió <[email protected]>
* @link                 https://github.com/rubensarrio/codeigniter-hmvc-doctrine
*/
class Doctrine {
    
  public $em = null;
  public $dm = null;
  public $tool = null;

  public function __construct()
  {    
    // Load database configuration from CodeIgniter
    require_once APPPATH.'config/database.php';
    
        // Set up class loading
    require_once APPPATH.'third_party/doctrine-orm/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php';

    $loader = new ClassLoader('Doctrine', APPPATH.'third_party/doctrine-orm');
    $loader->register();

    $classLoader = new ClassLoader('Doctrine\MongoDB', APPPATH.'third_party/doctrine-orm/vendor/doctrine-mongodb/lib');
    $classLoader->register();

    $classLoader = new ClassLoader('Doctrine\ODM', APPPATH.'third_party/doctrine-orm');
    $classLoader->register();

    $classLoader = new ClassLoader('Doctrine\Common',  APPPATH.'third_party/doctrine-orm/vendor/doctrine-common/lib');
    $classLoader->register();


        // Set up Gedmo
        $classLoader = new ClassLoader('Gedmo', APPPATH.'third_party');
        $classLoader->register();

        
        // Set up models loading
        $loader = new ClassLoader('models', APPPATH);
        $loader->register();
        foreach (glob(APPPATH.'modules/*', GLOB_ONLYDIR) as $m) {
            $module = str_replace(APPPATH.'modules/', '', $m);
            $loader = new ClassLoader($module, APPPATH.'modules');
            $loader->register();
        }
        

        $evm = new EventManager;
        // timestampable
        $evm->addEventSubscriber(new TimestampableListener);
        // sluggable
        $evm->addEventSubscriber(new SluggableListener);
        // tree
        $evm->addEventSubscriber(new TreeListener);
        
        // Set up proxies loading
    $loader = new ClassLoader('Proxies', APPPATH.'Proxies');
    $loader->register();
        
    // Set up caches
    $config = new Configuration;
    $cache = new ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // Set up driver
    $reader = new AnnotationReader($cache);
    $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
    
        // Set up models
        $models = array(APPPATH.'models');
        foreach (glob(APPPATH.'modules/*/models', GLOB_ONLYDIR) as $m)
            array_push($models, $m);
        $driver = new AnnotationDriver($reader, $models);

    $config->setMetadataDriverImpl($driver);

    // Proxy configuration
    $config->setProxyDir(APPPATH.'/Proxies');
    $config->setProxyNamespace('Proxies');
  
    $config->setAutoGenerateProxyClasses( TRUE );
        

    foreach ($db as $connection_name => $db_values) {

        if(!preg_match("/mongo/", $connection_name)) {

            // Database connection information
            $connectionOptions = array(
                'driver' => $db[$connection_name]['dbdriver'],
                'user' => $db[$connection_name]['username'],
                'password' => $db[$connection_name]['password'],
                'host' => $db[$connection_name]['hostname'],
                'dbname' => $db[$connection_name]['database']
            );
        
            // Create EntityManager
            $this->em[$connection_name] = EntityManager::create($connectionOptions, $config);

            // Force UTF-8
            $this->em[$connection_name]->getEventManager()->addEventSubscriber(
                new MysqlSessionInit('utf8', 'utf8_unicode_ci'));
            
            // Schema Tool
            $this->tool[$connection_name] = new SchemaTool($this->em[$connection_name]);

        
        } else {

            /**
            * MongoDB handler...
            */
            $configD = new MongoDBConfiguration();
            $configD->setProxyDir('<<PATH_TO_MONGODB_PROXIES>>');
            $configD->setProxyNamespace('Proxies');

            $readerD = new MongoAnnotationDriver(new AnnotationReader());
            $readerD->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\Driver\\');

            $configD->setMetadataDriverImpl(new MongoAnnotationDriver($readerD, APPPATH.'models/'));
    
            try { $this->dm[$connection_name] = DocumentManager::create(new Mongo("mongodb://".$db[$connection_name]['hostname']), $configD); } catch (Exception $e) { var_dump($e->getMessage()); }
        
        }

    }


  }
}

// END Doctrine class

/* End of file Doctrine.php */
/* Location: ./application/libraries/Doctrine.php */

Thanks
#7

[eluser]Unknown[/eluser]
We are just a learner for doctrine and mongodb.
We need to work with codeigniter, Doctrine and Mongodb.

From here, How can we use this in model and controller.




Theme © iAndrew 2016 - Forum software by © MyBB