Entity manager for specified entities - symfony

I have multiple managers and configuration of one of them is like this
doctrine:
orm:
entity_managers:
support:
connection: support
mappings:
APIBundle: ~
But there are tens of entities in APIBundle and I need only some of them in this manager. What a correct configuration should be in such case?

I can't find anything in the doctrine documentation which match with what you're asking.
But by reading the mapping part, we can imagine a little trick to pass through that :
Define your entities in two distinct folders :
APIBundle
|
--- Em1Entity
|
--- SupportEntity
Then in your config, specify the dir configuration :
doctrine:
orm:
entity_managers:
support:
connection: support
mappings:
Support:
mapping: true
type: ~
dir: APIBundle\SupportEntity
alias: ~
prefix: ~
is_bundle: ~
It's just a guess, I didn't personnaly tested this hack.

Related

Create doctrine orm setting for only 1 entity in symfony

Im creating a config in my symfony 3.4 that allow to access multiple database
I have set default to automapping to 1 database
then i would like to create 1 orm setting to only 1 entity, not 1 bundle, is that possible?
currently it look like this:
connectionName:
connection: connName
mappings:
IpSaBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
When I did the above, I got an error, because in that bundle I have another entity that belongs to default orm setting
Yes it's possible.
Check here: https://symfony.com/doc/3.4/reference/configuration/doctrine.html#mapping-entities-outside-of-a-bundle
# ...
orm:
# ...
mappings:
# ...
SomeEntityNamespace:
type: annotation
dir: '%kernel.project_dir%/src/Entity'
is_bundle: false
prefix: App\Entity
alias: App

Symfony: The class XYZ was not found in the chain configured namespaces

Existing questions did not help.
I am still learning Symfony and setting up an existing project. Trying to run doctrine fixtures from my application directory.
./app/console doctrine:fixtures:load --env test
And running this gives me following errors
Error thrown while running command "doctrine:fixtures:load --env test --em default". Message: "The class 'ClientPortal\GenericBundle\Document\Accountant' was not found in the chain configured namespaces _XyzProjectName\CalendarContext\Domain\Model, Integration\GetFeedbackBundle\Model, _XyzProjectName\AccountingProcessContext\Domain\Model, _XyzProjectName\CaseContext\Domain" {"exception":"[object] (Doctrine\Common\Persistence\Mapping\MappingException(code: 0): The class 'ClientPortal\GenericBundle\Document\Accountant' was not found in the chain configured namespaces _XyzProjectName\CalendarContext\Domain\Model, Integration\GetFeedbackBundle\Model, _XyzProjectName\AccountingProcessContext\Domain\Model, _XyzProjectName\CaseContext\Domain at /usr/local/var/www/1800-api/vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:22)
And this exception is being thrown when trying to persist object of class
'ClientPortal\GenericBundle\Document\Accountant'
and here is my doctrine in config.yml
doctrine:
dbal:
driver: pdo_pgsql
host: '%database_host%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
CalendarBundle:
prefix: '_XyzProjectName\CalendarContext\Domain\Model'
IntegrationGetFeedbackBundle:
prefix: 'Integration\GetFeedbackBundle\Model'
AccountingProcessBundle:
prefix: '_XyzProjectName\AccountingProcessContext\Domain\Model'
CaseBundle:
prefix: '_XyzProjectName\CaseContext\Domain'
GenericBundle:
type: "annotation"
prefix: 'ClientPortal\GenericBundle\Document'
dir: 'src'
is_bundle: false
The snippet which is raising this exception
$account = new ClientPortal\GenericBundle\Document\Accountant();//full namespace added for clarity - also this file lies in the same directory structure.
$account->setSfAccountantId($accountantId);
$account->setUsername($username);
$manager = new ObjectManager;
$manager->persist( $account ); //This throws the above mentioned exception
My thought is, the file ClientPortal\GenericBundle\Document\Accountant.php is being autoloaded as there is no exception thrown at the time of instantion of its object. But, there is something missing with configuration or mapping because of which doctrine does not know how to persist its object.
[Source code][1] Accountant class
GenericBundle(s) registered under AppKernel
new Website\GenericBundle\WebsiteGenericBundle(),
new Admin\GenericBundle\AdminGenericBundle(),
new ClientPortal\GenericBundle\ClientPortalGenericBundle(),
new TaxApp\GenericBundle\TaxAppGenericBundle(),
So, as I suggested in comments - try to add ClientPortal\GenericBundle\Document\ namespace to mappings in your doctrine config file, like that:
#...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
mappings:
# ...
ClientPortalGenericBundle:
type: annotation
prefix: 'ClientPortal\GenericBundle\Document\'
dir: '%kernel.root_dir%/src/ClientPortal/GenericBundle/Document/'
#is_bundle: false
auto_mapping: true
Had the same issue with Symfony 4 while using multiple database connections. This one https://stackoverflow.com/a/55361742/2540699 helped me to solved my problem.
I bumped into this kind of message in my Symfony 6.1 project, using multiple entity managers, with one of the entity managers specified as default in doctrine.yaml:
doctrine:
orm:
default_entity_manager: general
entity_managers:
general:
# config for general
other_entity_manager:
# config for other entity manager
Everything was fine on my dev environment, but on production I got the error
Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class XXX was not found in the chain configured namespaces " at /opt/approot/current/vendor/doctrine/persistence/src/Persistence/Mapping/MappingException.php
Problem seemed to be that I have a when#prod: section in my doctrine.config.yml. It seems I had to repeat the default_entity_manager over there:
when#prod:
doctrine:
orm:
default_entity_manager: general
# other prod-specific configuration
This fixed it for me.
Update: This is probably related to this issue: https://github.com/symfony/symfony/issues/27769

Amazon Aurora db with Doctrine

I need some help, I was working with mysql and doctrine and all was perfect, but now I'm using Auroradb which uses two instances (reader and writer).
At first I tried to use two entity managers, one for writing and other for reading, but I got a problem with SyliusRbacBundle.
so, is there another way to use aurora and doctrine?????
UPDATE 1
this is the error that I get after using Daniel's config
A new entity was found through the relationship 'Litigon\UserBundle\Entity\User#authorizationRoles' that was not configured to cascade persist operations for entity: SuperAdministrador. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #ManyToOne(..,cascade={"persist"}).
so, if I merge the default entity manager as a lot of people suggest, I get problems with aurora 'cause of the other manager is for the reader instance and then when flushing aurora says that isn't allowed to write.
You need to specify where the models or entities actually live in doctrine config, also is important to notice that Sylius models are usually located on the component and not in the bundle. Finally, but not least important, can only have one connection with auto mapping:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
loggable:
type: annotation
alias: Gedmo
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
prefix: FOS\UserBundle\Model
SyliusRbacBundle:
type: xml
dir: Resources/config/doctrine/model
prefix: Sylius\Component\Rbac\Model
SyliusResourceBundle: ~
OtherBundle: ~
writer:
connection: writer
mappings:
loggable:
type: annotation
alias: Gedmo
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
prefix: FOS\UserBundle\Model
SyliusRbacBundle:
type: xml
dir: Resources/config/doctrine/model
prefix: Sylius\Component\Rbac\Model
SyliusResourceBundle: ~

How to install DoctrineExtensions in Symfony 2.5 and latest

I need use mysql functions in qb such as DATE(), NOW(), FROM_UNIXTIME() etc. I reading this topic How to install Doctrine Extensions in a Symfony2 project. I did it:
composer require "beberlei/DoctrineExtensions":"dev-master"
all became normal:
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing beberlei/doctrineextensions (dev-master 5e4ec9c)
Cloning 5e4ec9c3ec3434151e1c73144b4ab87ae30aefbc
etc...
Đ•hen I did it:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
BUT! Symfony return this exception for me:
FileLoaderLoadException: Cannot import resource "/home/domain/www/domain.com/app/config/config.yml" from "/home/domain/www/domain.com/app/config/config_dev.yml". (A YAML file cannot contain tabs as indentation at line 106 (near " dql:").)
Sorry for my english, but in russia internet segment no normal specialists for Symfony2 framework. I hope for your help, my friends!
From your error:
A YAML file cannot contain tabs as indentation at line 106 (near " dql:")
You have tabs in this file:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
Instead tabs create spaces.

dependency on a non-existent service "doctrine.orm.default_entity_manager"

I am using JMSPaymentCoreBundle and JMSPaymentPaypalBundle.
It worked well before,but now I have to change my config.yml for new Bundle(FOSMessageBundle)
I have to stop using 'auto_mapping' and use 'entity_managers' instead
doctrine:
dbal:
orm:
auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
entity_managers:
FOSUserBundle: ~
FOSMessageBundle: ~
However after this changes .
The service "payment.plugin_controller" has a dependency on a non-existent service "doctrine.orm.default_entity_manager"
this error happens.
I think changes in config.yml causes this trouble.
How can I solve this problem?
According to the error, you need to define an entity manager named default. In your case, the overall syntax is just wrong, see my example.
In config.yml:
doctrine:
orm:
entity_managers:
default: # that's the name of the entity manager
connection: default # you need to define the default connection
mappings:
FOSUserBundle: ~
FOSMessageBundle: ~
I'd advise you to read the documentations about "Databases and Doctrine" and "How to work with Multiple Entity Managers and Connections"

Resources