Zend Framework 2 and Doctrine 2 - Configuration for multiple databases - php

I pasted the code from the configuration.md file to
module.config.php
'doctrine' => array(
'connection' => array(
'orm_crawler' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'crawler',
'driverOptions' => array(
1002 => 'SET NAMES utf8'
),
)
)
),
'configuration' => array(
'orm_crawler' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'driver' => 'orm_crawler',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
'filters' => array()
)
),
'driver' => array(
'Crawler_Driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Crawler/Entity'
)
),
'orm_crawler' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain',
'drivers' => array(
'Crawler\Entity' => 'Crawler_Driver'
)
),
),
'entitymanager' => array(
'orm_crawler' => array(
'connection' => 'orm_crawler',
'configuration' => 'orm_crawler'
)
),
'eventmanager' => array(
'orm_crawler' => array()
),
'sql_logger_collector' => array(
'orm_crawler' => array(),
),
'entity_resolver' => array(
'orm_crawler' => array()
),
),
```
Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'doctrine.authenticationadapter.orm_crawler' => new \DoctrineModule\Service\Authentication\AdapterFactory('orm_crawler'),
'doctrine.authenticationstorage.orm_crawler' => new \DoctrineModule\Service\Authentication\StorageFactory('orm_crawler'),
'doctrine.authenticationservice.orm_crawler' => new \DoctrineModule\Service\Authentication\AuthenticationServiceFactory('orm_crawler'),
'doctrine.connection.orm_crawler' => new \DoctrineORMModule\Service\DBALConnectionFactory('orm_crawler'),
'doctrine.configuration.orm_crawler' => new \DoctrineORMModule\Service\ConfigurationFactory('orm_crawler'),
'doctrine.entitymanager.orm_crawler' => new \DoctrineORMModule\Service\EntityManagerFactory('orm_crawler'),
'doctrine.driver.orm_crawler' => new \DoctrineModule\Service\DriverFactory('orm_crawler'),
'doctrine.eventmanager.orm_crawler' => new \DoctrineModule\Service\EventManagerFactory('orm_crawler'),
'doctrine.entity_resolver.orm_crawler' => new \DoctrineORMModule\Service\EntityResolverFactory('orm_crawler'),
'doctrine.sql_logger_collector.orm_crawler' => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('orm_crawler'),
'doctrine.mapping_collector.orm_crawler' => function (\Zend\ServiceManager\ServiceLocatorInterface $sl) {
$em = $sl->get('doctrine.entitymanager.orm_crawler');
return new \DoctrineORMModule\Collector\MappingCollector($em->getMetadataFactory(), 'orm_crawler_mappings');
},
'DoctrineORMModule\Form\Annotation\AnnotationBuilder' => function(\Zend\ServiceManager\ServiceLocatorInterface $sl) {
return new \DoctrineORMModule\Form\Annotation\AnnotationBuilder($sl->get('doctrine.entitymanager.orm_crawler'));
},
),
);
}
I'm getting the following error:
C:\xampp\vhosts\zf2-trade\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:529
Message:
Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getServiceManager
What am I doing wrong? Please help.
Regards Matthew

Mac, welcome to stackoverflow! You don't need to define custom factories for each connection respectively. DoctrineORMModule already handles this job for us.
When you need the entity managers, get it from service locator instance by using their names in the alias like this:
$this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
or
$this->getServiceLocator()->get('doctrine.entitymanager.orm_alternative');
I'm sharing one of my current application's database configuration which currently uses both PostgreSQL and MySQL connections.
<?php
return array(
'doctrine' => array(
'connection' => array(
// Default DB connection
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
'params' => array(
'host' => '1.2.3.4',
'user' => 'pdbuser',
'port' => '5432',
'password' => '****',
'dbname' => 'mydb',
'driver' => 'pdo_pgsql',
),
),
// Alternative DB connection
'orm_alternative' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '4.5.6.7',
'user' => 'dbuser',
'port' => '3306',
'password' => '****',
'dbname' => 'mydb',
'driver' => 'pdo_mysql',
),
),
),
// Entity Manager instantiation settings
'entitymanager' => array(
'orm_default' => array(
'connection' => 'orm_default',
'configuration' => 'orm_default',
),
'orm_alternative' => array(
'connection' => 'orm_alternative',
'configuration' => 'orm_alternative',
),
),
// Use array cache locally, also auto generate proxies on development environment.
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
),
'orm_alternative' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
),
),
),
);
You can easily merge this configuration with yours.
Hope it helps.

#edigu answer works perfectly fine but in some cases, it gives
"Following error: Zend\ServiceManager\ServiceManager:: get was unable to fetch or create an instance for doctrine.connection.orm_crawle"
So to resolve this we may change in the Entity Manager settings
'entitymanager' => array(
'orm_default' => array(
'connection' => 'orm_default',
'configuration' => 'orm_default',
),
'orm_alternative' => array(
'connection' => 'orm_alternative',
'configuration' => 'orm_default', //<--use parent configurations
),
),
for refernece check here

#foozy: this is exactly what I'm looking. With
./vendor/bin/doctrine-module orm:schema-tool:update --force
you can now update or create the database schema.
My question: how to define which entity should be created in which db?
Regards Andrea

Related

Generating Doctrine 2 entities with multiple database connections in Zend Framework 2

For a web application, I have to work with 3 seperate databases:
MySQL for all the application logic, such as login
MSSQL Server for inserting records
Oracle server for inserting records
Now i have read a lot of tutorials and manuals to configure those databases, so it now looks like this:
doctrine.global.php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '123.123.123.123',
'port' => '3306',
'dbname' => 'db_name',
),
),
'orm_oracle' => array(
'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver',
'params' => array(
'host' => '321.321.321.321',
'port' => '1521',
'dbname' => 'something',
'driver' => 'oci8',
'servicename' => 'something',
),
),
'orm_microsoft' => array(
'driverClass' => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
'params' => array(
'host' => '231.231.231.231',
'port' => '1433',
'dbname' => 'something',
'driver' => 'sqlsrv',
),
),
)
)
);
doctrine.local.php*
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'user' => 'brportal',
'password' => '27607097b4',
),
),
//Alternative DB connection
'orm_oracle' => array(
'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver',
'params' => array(
'user' => 'joost',
'password' => '4b58tL8DFv7G',
),
),
'orm_microsoft' => array(
'driverClass' => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
'params' => array(
'user' => 'joost',
'password' => '4b58tL8DFv7G',
),
),
),
'entitymanager' => array(
'orm_default' => array(
'connection' => 'orm_default',
'configuration' => 'orm_default',
),
'orm_oracle' => array(
'connection' => 'orm_oracle',
'configuration' => 'orm_oracle',
),
'orm_microsoft' => array(
'connection' => 'orm_microsoft',
'configuration' => 'orm_microsoft',
),
),
// Use array cache locally, also auto generate proxies on development environment.
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
),
'orm_oracle' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
),
'orm_microsoft' => array(
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => true,
),
),
)
);
Module.php
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '\..\src\\' . __NAMESPACE__ . '\Entities')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entities' => __NAMESPACE__ . '_driver'
)
),
'oracle_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '\..\src\\' . __NAMESPACE__ . '\Entities\Oracle')
),
'orm_oracle' => array(
'drivers' => array(
__NAMESPACE__ . '\Entities\Oracle' => 'oracle_driver'
)
),
'microsoft_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '\..\src\\' . __NAMESPACE__ . '\Entities\Microsoft')
),
'orm_microsoft' => array(
'drivers' => array(
__NAMESPACE__ . '\Entities\Microsoft' => 'microsoft_driver'
)
)
)
)
But now, i would really like to have a easy way to (re)generate the mssql and oracle database entities (annotations). It already worked with the MySQL database, but i can't find out how to specify a database connection with doctrine-module orm:convert-mapping.
I was wondering if anyone can help me with this?
Thanks in advance guys!
ps: I already read :
Zend Framework 2 and Doctrine 2 - Configuration for multiple databases
The bottom line is that it is not supported in the ZF2 DoctrineORM module. There is a feature request for it on the repo.
In the meantime there is a module that provides a shim module that add the --em option
https://github.com/SwissEngine/Doctrine-Module-Extension

Class 'MongoId' not found (Zend Framework with MongoDB Doctrine)

Im' currently trying to integrate MongoDB with Doctrine in ZendFramework. I did a lot of tutorial (on StackOverflow or anywhere else) but nothing is really working.
I followed step by step a tutorial: http://www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm and I got an error that I don't understand.
Fatal error: Class 'MongoId' not found in /home/prooxi/www/zframework/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/IdType.php on line 38
The IdType.php is source code for the mongoDB, so the error must be somewhere else.
Here is the files I have. (Admin is the name of the module)
config/application.config.php
<?php
return array(
'modules' => array(
'Application',
'DoctrineModule',
'DoctrineMongoODMModule',
'Udmin',
'Listing',
'Admin',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
),
);
config/autoload/module.doctrine-mongo-odm.local.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'MYDBADRESS',
'port' => '27017',
/* 'connectionString' => null, */
/* 'user' => null, */
/* 'password' => null, */
'dbname' => 'px_boutique_test27',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
/* 'proxy_dir' => __DIR__ . '/module/Admin/src/Admin/Model/Proxy', */
/* 'proxy_dir' => __DIR__ . '/module/Udmin/src/Udmin/Model/Proxy', */
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
/* 'proxy_namespace' => 'Udmin\Model\Proxy', */
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
/* 'hydrator_dir' => __DIR__ . '/module/Udmin/src/Udmin/Model/Hydrator', */
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
/* 'hydrator_namespace' => 'Udmin\Model\Hydrator', */
'default_db' => 'test27',
'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
/* 'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack' */
)
),
'driver' => array(
'odm_default' => array(
'drivers' => array(
'Admin\Document' => 'aplikasi'
)
),
'aplikasi' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'module/Admin/src/Admin/Document'
)
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
Module/Admin/Src/Admin/Controller/AdminController.php
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Mongo;
use Zend\Session\SaveHandler\MongoDB;
use Zend\Session\SaveHandler\MongoDBOptions;
use Zend\Session\SessionManager;
use Admin\Document\Boutique;
class AdminController extends AbstractActionController
{
public function indexAction()
{
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$b = new Boutique();
/* $dm->getRepository('Admin\Document\Boutique')->findAll(); */
$dm->find('Admin\Document\Boutique', '52e6c677362dca7fcd40ab09');
}
}
Module/Admin/config/module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
),
),
);
The purpose of the module is to connect to an existing MongoDB databate and just make a list of all the document in it.
Thank you !
Gilles
If you are using the newer mongodb extension rather than mongo extension for PHP you will need to use MongoDB\BSON\ObjectID as detailed on the php.net page

zend framework 2 + doctrine2 + DoctrineDataFixtureModule no loading fixtures

hi have some problems with zend framework 2 + doctrine2 and DoctrineDataFixtureModule (https://github.com/Hounddog/DoctrineDataFixtureModule)
the module is not loading my fixtures
this is my config
'doctrine' => array(
'eventmanager' => array(
'orm_default' => array(
'subscribers' => array(
'Gedmo\Tree\TreeListener',
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
'Gedmo\Loggable\LoggableListener',
'Gedmo\Sortable\SortableListener'
),
),
),
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/'.__NAMESPACE__.'/Entity',
)
'data-fixture' => array(
__NAMESPACE__.'_fixture' => __DIR__ . '/../src/'.__NAMESPACE__.'/Fixtures',
),
),
'translatable_metadata_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity',
),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__.'\Entity' => __NAMESPACE__.'_driver',
// 'Gedmo\Translatable\Entity' => 'translatable_metadata_driver',
)
),
)
),
the problem is i dont know how set the fixtures path or what exactly key in the array i have to set the fixtures path
in the documentation the developer says:
To register drivers with Doctrine module simply add the drivers to the
doctrine.driver key in your configuration.
return array(
'data-fixture' => array(
'ModuleName_fixture' => __DIR__ . '/../src/ModuleName/Fixture',
),
);
You actually need to add the data-fixture array to the root of the configuration array, not in the doctrine array. Like so:
return array(
...
'doctrine' => array(
'eventmanager' => array(
'orm_default' => array(
'subscribers' => array(
'Gedmo\Tree\TreeListener',
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
'Gedmo\Loggable\LoggableListener',
'Gedmo\Sortable\SortableListener'
),
),
),
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/'.__NAMESPACE__.'/Entity',
)
),
'translatable_metadata_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity',
),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__.'\Entity' => __NAMESPACE__.'_driver',
// 'Gedmo\Translatable\Entity' => 'translatable_metadata_driver',
)
),
)
),
'data-fixture' => array(
__NAMESPACE__.'_fixture' => __DIR__ . '/../src/'.__NAMESPACE__.'/Fixtures',
),
...
);
I'll admit the wording in the documentation is a bit confusing.

mongo-odm configuration (module / global) in zf2

I was following this tutorial
http://www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm
when a get this error:
The class 'Application\Document\User' was not found in the chain configured namespaces
This is my module.doctrine-mongo-odm.local.php after a little bit of testing:
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => '',
'password' => '',
'dbname' => 'test',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => 'test',
'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),
'odm_default' => array(
'drivers' => array(
'Application\Document' => 'odm_driver'
)
),
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'module/Application/src/Application/Document'
),
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
I could fix the error by adding this information to the the Application/config/module.config.php and remove it from the global conf:
<?php
namespace Application;
return array(
// routes, etc
'doctrine' => array(
'driver' => array(
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => 'odm_driver'
)
)
)
)
Can you explain me, why this is working? And what is the best way to go, since i need the odm in different modules? Define it in every module.config.php where its needed?
Played a little with the configs I managed to setup Global configruation.
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
Change is here --> 'Admin\Document' => 'odm_driver'
)
)
As you can see, I've changed NAMESPACE to strict value, this made the trick. Really don't understand till the end new Namespaces in the ZF2.

In yii,why can't I get all parameters using yii::app()->getParams() method?

In my program,I called getParams() method,but only got 10 parameters.In my main.php configuration file,there is more,why?
Here is the params part of the main.php
'params'=>array(
'adminEmail'=>'webmaster#example.com',
'AccountDB' => 'db_account_dev',
'orderDB' => 'db_order_dev',
'staticPath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'commonDB' => 'db_common',
'payDB' => 'db_pay_dev',
'crmDB' => 'db_crm_dev',
'queue' => array(
'email' => array(
array(
'host' => '127.0.0.1',
'port' => 11300,
),
),
'sms' => array(
array(
'host' => '127.0.0.1',
'port' => 11330,
),
),
'sale_record' => array(
array(
'host' => '192.168.0.201',
'port' => 11300,
),
),
),
'discuz' => array(
'DBName' => 'db_ultrax',
'TablePrefix' => 'pre_',
'authKey' => '00fefan5JEvKdiEQ',
),
'freight' => require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'freight.php'),
'allowCheckTaskSubId' => 224,
'allowCheckEmail' => 242,
'allowCheckPass' => 243,
'codFee' => 10,
'cancelFee' => 10,
)
However,the method only returns some items
adminEmail=>webmaster#example.com
orderDB=>db_order_dev
payDB=>db_pay
AccountDB=>db_account
commonDB=>db_common
freight=>Array
codFee=>10
saleRoleId=>9
saleManager=>11
growth=>Array
I have got the answer.
The configuration file of console application is protected/config/console.php
while the configuration file of web application is protected/config/main.php
They are different!
The above codes from a console application

Categories