I use doctrine2 with ZF2, some of my libraries work with Zend\Db\Adapter\Adapter, others with doctrine2. Now, they connect to database twice. Is it possible to use one db connection in doctrine and standard ZF2 db adapter?
相关问题
- Why does recursive submodule update from github fa
- How to pass options/params to formCollection field
- Lifecycle Callback Issue When Extending FOSUserBun
- Compress html output from zend framework 2
- Doctrine 2.5: Unrecognized field (but only in Symf
相关文章
- Symfony : Doctrine data fixture : how to handle la
- Is there a way to modify the entity mapping config
- Get random records with Doctrine
- Doctrine not finding data on Google App Engine?
- Doctrine 2: Cannot select entity through identific
- Symfony does not remove entity from collection
- Symfony doctrine:schema:update not working
- Storing Doctrine2 DateTime in UTC Only
Sorry for posting this as new answer but I am not able to add a comment to Crisp's answer since my reputation is too low because I only registered to stackoverflow for writing this comment:
In the
dbconn.local.php
that Crisp posted be sure to setdbname
tonull
like in the following snippet:Addition to Crisp's answer:
And now here is why this is important:
When calling
on your
EntityManager
without having set thedbname
tonull
you will get"database"
as the name of your database because this is the default value which is set by themodule.config.php
of theDoctrineORMModule
as you can see here. Setting thedbname
tonull
will cause yourDoctrine\DBAL\Driver\PDOMySql\Driver
which extendsDoctrine\DBAL\Driver\AbstractMySQLDriver
to load the name of the database viaSELECT DATABASE()
from the database itself as you can see here.Also not setting the
dbname
tonull
(or to the correct database name) will cause theschemaInSyncWithMetadata()
function of theDoctrine\ORM\Tools\SchemaValidator
to always return false since it cannot load the current database setup because it uses theDoctrine\ORM\Tools\SchemaTool
which uses theEntityManager
'sConnection
which thinks that the database being used is called"database"
.So I hope someone can use this information to save some time. I wasted half the day to figure that out.
And many thanks to Crisp again for his answer that saved me a lot of time.
The
DoctrineORM
module accepts aPDO
resource or a service name where the instance can be located in the service manager instead of the usual connection params.First step is to create a service factory which retrieves the PDO resource from the
Zend\Db\Adapter\Adapter
serviceOnce you have the factory, it's just a case of adding it to the service manager, configuring the db params for
Zend\Db\Adapter\Adapter
and telling doctrine to use the existingPdoResource
from the service manager to connect.Assuming you did this all in one file, let's say
dbconn.local.php
...