I have two MySQL connections set in my Symfony config.yml:
doctrine:
dbal:
default_connection: default
connections:
default: __VARIABLES
dealer: __VARIABLES
I know I can retrieve objects from certain connection by:
$this->get('doctrine')->getEntityManager('dealer');
My question is - is there a way to set connection to certain entity, so the doctrine:schema:update syncing is done only in the scope of the set connection?
Yes - you can specify in entity manager section entity manager's responsibility. You can use "dir" option for this purpose. You have to disable auto_mapping options and tell Doctrine which entities are related to which entity manager. For example:
doctrine:
dbal:
(...)
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
CompanySomeBundle:
dir: Entity/BaseEntity/
secondem:
connection: second_connection
mappings:
CompanySomeBundle:
dir: Entity/AdditionalEntity/
In that every entity in BaseEntity dir belongs to "default" em, and these entities from AdditionalEntity dir belong to "secondem" entity manager.
Related
In my bundle i create a separate database connection and an EntityManager for it. Everything works fine, except those two things don't show up in the development profiler. There is only the default EntityManager and the default connection.
So basically i created 3 new service definitions for an Doctrine\Common\EventManager, an Doctrine\DBAL\Connection and an Doctrine\ORM\EntityManager. I've already tried to add these new service definition to the ContainerBuilder with the same naming convention which is used by the doctrine bridge, but they still won't show up in the profiler. The connection works fine, but i want debug it with and integrate it in the Symfony lifecycle.
The question is:
What is the best practice to create a separate database connection via Doctrine inside of a Symfony Bundle if the Symfony application is only configured to support one connection?
I believe you should take a look at this doc. They described there how to add another EntityManager, which mean another connection. First step is to create configuration.
Especialy take a look at doctrine.yaml configuration:
# config/packages/doctrine.yaml
doctrine:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer:
connection: customer
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
Above are two entity managers, 'default' and 'customer'. There are also two cennections, one for each manager.
If configuration is valid you will have access to those managers by passing its names to 'getManager' method.
$entityManager = $this->getDoctrine()->getManager('default');
$customerEntityManager = $this->getDoctrine()->getManager('customer');
If you cant edit configuration:
What what about creating custom class (Manager or something) in which you will manually create connection. Take a look at this, it should help you.
getting-a-connection
According to the Symfony docs, multiple entity managers can be configured by listing them in config/packages/doctrine.yaml then selecting a specific entity manager in a controller like this: $this->getDoctrine()->getManager('customer');.
I set up my config/packages/doctrine.yaml:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '8.0'
charset: utf8mb4
meter:
# configure these for your database server
url: '%env(DATABASE_METER_URL)%'
driver: 'pdo_mysql'
server_version: '8.0'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
meter:
connection: meter
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
and then tried to make a query with
$meterEntityManager = $this->getDoctrine()->getManager('meter');
$meter = $meterEntityManager->getRepository(Entity\Meter::class)->find($meterId);
However, the result of $meterEntityManager->getRepository(Entity\Meter::class)->find($meterId) is a row from the database connection named default, not the one named meter -- it is as if the code had been
$entityManager = $this->getDoctrine()->getManager('default');
$meter = $entityManager->getRepository(Entity\Meter::class)->find($meterId);
When I check the connection with $meterEntityManager->getConnection()->getParams() it correctly lists the params for the meter connection rather than the default connection, so I can get a row from the meter database connection by executing queries directly on the connection object:
$meterEntityManager = $this->getDoctrine()->getManager('meter');
$stmt = $meterEntityManager->getConnection()->prepare('SELECT * FROM meter WHERE id = :id');
$stmt->execute(['id' => $meterId]);
$meter = $stmt->fetch();
But ultimately I want to fetch rows through the methods offered by getRepository(Entity\Meter::class) so returned data is automatically hydrated into doctrine entities. There seems to be an inconsistency between the connection used by $meterEntityManager->getRepository(Entity\Meter::class) and $meterEntityManager->getConnection(). Why is this? Does it have something to do with the entity managers using different connections but sharing the same mappings to entities?
The basic issue here is that ServiceEntityRepository class iterates through a list of entity managers and uses the first one that supports a given entity. Which is fine except when you happen to have entities supported by multiple entity managers. The solution is to go "old school" and extend from the EntityRepository class instead of what is basically a container aware ServiceEntityRepository.
use Doctrine\ORM\EntityRepository;
class MeterRepository extends EntityRepository
{
// Note: Do not override the constructor here
You also have to add the App\Repository directory to the list of excluded directories in services.yaml.
The reason for the ServiceEntityRepository was to allow the Symfony container to autowire repositories and inject them directly into other services. It also allows the developer to inject additional dependencies into the repository if they so chose.
tl;dr How does the getManagerForClass() method find out which entity manager is the right one for a specific class?
I've made a generic controller that should be able to handle basic actions for different entities.
I also have connections to two different databases, so I'm using two entity managers.
In my controller, I'm trying to use Doctrine's getManagerForClass() method to find which manager to use for each class, as explained on this blog and this SO answer.
But the method does not seem to differentiate my two entity managers and simply returns the first one in the configuration.
My controller action starts like this:
public function indexAction($namespace, $entityName)
{
$classFullName = "AppBundle:$namespace\\$entityName";
$em = $this->getDoctrine()->getManagerForClass($classFullName);
This is my Doctrine configuration:
dbal:
default_connection: postgres
connections:
postgres:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
oracle:
driver: oci8
host: "%oracle_host%"
port: "%oracle_port%"
dbname: "%oracle_name%"
user: "%oracle_user%"
password: "%oracle_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: true
entity_managers:
postgres:
connection: postgres
mappings:
AppBundle:
type: annotation
dir: Entity\Postgres
oracle:
connection: oracle
mappings:
AppBundle:
type: annotation
dir: Entity\Oracle
And my folder structure is as follows:
AppBundle
|___Controller
| |___EntityController.php
|
|___Entity
|___Postgres
| |___SomePostgresBasedEntity.php
|
|___Oracle
|___SomeOracleBasedEntity.php
Now I don't know exactly how the method works, and how it it supposed to know about the mapping if not through the configuration.
But if I call it this way, for example:
$em = $this->getDoctrine()->getManagerForClass("AppBundle:Oracle\\SomeOracleBasedEntity");
...I get the entity manager for Postgres.
But if I simply switch the entity manager configuration, putting the one for oracle first, the previous call works, but the following doesn't:
$em = $this->getDoctrine()->getManagerForClass("AppBundle:Postgres\\SomePostgresBasedEntity");
Update 1
getManagerForClass() cycles through every manager and for each one, checks if the class is "non-transient":
foreach ($this->managers as $id) {
$manager = $this->getService($id);
if (!$manager->getMetadataFactory()->isTransient($class)) {
return $manager;
}
}
This goes all the way down to AnnotationDriver->isTransient(). Here the doc says the following:
A class is non-transient if it is annotated with an annotation from the AnnotationDriver::entityAnnotationClasses.
#Entity seems to be one of those annotations that makes a class non-transient.
But then, how could any of my entities be transient at all? How could the driver distinguish an entity that belongs to a specific manager based solely on its annotations?
I must have missed something in the higher level classes.
Update 2
The method works when using yml mappings.
I kind of expected this behaviour. The difference comes from the implementations of the isTransient() method in the different drivers. The FileDriver implementation of isTransient returns true if the metadata file exists in the dir: directory of the mapping configuration.
I would have expected the AnnotationDriver to search for annotations only in the entities contained in the specified dir: directory, but it seems to ignore that parameter.
Or should I use another one?
At long last, I solved it.
The solution was using the prefix parameter.
entity_managers:
postgres:
connection: postgres
mappings:
AppBundle:
type: annotation
dir: Entity\Postgres
prefix: AppBundle\Entity\Postgres
alias: Postgres
oracle:
connection: oracle
mappings:
AppBundle:
type: annotation
dir: Entity\Oracle
prefix: AppBundle\Entity\Oracle
alias: Oracle
Explanation
The prefix parameter gets passed to the corresponding Entity Manager service, and is added to the entityNamespaces property, which otherwise defaults to AppBundle/Entity.
The Annotation Driver will then check for annotations in that specific namespace, whereas the File Driver checks for existing mapping files in the directory specified through the dir parameter.
(The alias parameter is not mandatory.)
At least, that's how I understand it.
NOTE: Its not a Duplicate issue cause I have tried everything I get on google but nothing have helped us.
I am trying to import Tables using Doctrine Reverse Engineering tool, but I m getting this message:
Database does not have any mapping information.
My Connection Details in Config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: null
dbname: pixel_ashish
user: root
password: abc123
charset: UTF8
schema_filter: ~^(?!some_table1|some_table2)~
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
proxy_namespace: Proxies
resolve_target_entities: []
What I have tried So Far:
Running php app/console doctrine:mapping:import --force AcmeBlogBundle xml
gives same error
Tried to convert mapping also which does not make any sense cause mappings are not there but still tried didn't worked out.
Created a new project and tried above given configuration didn't worked.
Now I am out of ideas please help me to solve this.
You need to create some mapping information in your config.
You can do it by passing auto_mapping: true under orm section:
doctrine:
orm:
auto_mapping: true
or by manually defining it under orm section:
doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundleName:
type: "xml"
dir: "Entity"
prefix: "Your\BundleName\Entity"
I am having difficulty configuring the Doctrine Extension Taggable provided here:
https://github.com/FabienPennequin/DoctrineExtensions-Taggable
My project is using Symfony 2 Fullstack and my configuration is using yaml while my doctrine entities are using annotation. I installed DoctrineExtensions using composer. Adding "fpn/doctrine-extensions-taggable": "dev-master" to the require section on composer.json and then running composer update. This installed without issue.
I then become lost at this section: https://github.com/FabienPennequin/DoctrineExtensions-Taggable#setup-doctrine
I understand that the metadata is a Doctrine Entity however as previously mentioned I am using yaml for my symfony configuration as well as entity managers. Here is the excerpt from my config.yml file:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: main
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
main:
connection: default
mappings:
VendorMainBundle:
prefix: Vendor\MainBundle\Entity
taggable:
connection: default
mappings:
taggable:
type: xml
prefix: DoctrineExtensions\Taggable\Entity
dir: %kernel.root_dir%/../vendor/fpn/doctrine-extensions-taggable/metadata
However, when I run php app/console doctrine:mapping:info --em=taggable I get the error:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
Should the above command show the mappings described in the xml files?
Thereby allowing me to update the schema in the database?
I used this documenation as reference for the config.yml file: http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration
I also added this under the config.yml in order to setup the TagListener. Is this correct?
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
EDIT [#Grimv01k]:
The TagListener requires an argument passed that is an instance of the TagManager Object. I created another service to handle that as follows and passed it to the TagListener:
tag.manager:
class: DoctrineExtensions\Taggable\TagManager
tags:
- { name: doctrine.event_subscriber, connection: default }
arguments:
entity.manager: #doctrine.orm.entity_manager
taggable:
class: DoctrineExtensions\Taggable\TagListener
arguments:
manager: #tag.manager
The TagManager requires an argument of the entityManager however by doing so results in error:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".
Across the web it's recommended to resolve this error by passing #service_container and in the constructor of the object pull out the entity_manager, however being a vendor I'd prefer not to modify their code. Is there another way?
Just guess: maybe that happens becuse you haven't got tags applied in service, and Doctrine doesn't use this in your complier pass. Try to do it like this:
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
tags:
- { name: doctrine.event_subscriber, connection: default }