I'm looking to be able to get the correct Entity Manager given an entity instance (or just classname).
It was pointed out that doctrine ManagerRegistry has the method getManagerForClass, which accepts a classname and should return the correct matching Entity Manager.
However, when I call it, it always returns the default.
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
gedmo_loggable:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity'
prefix: Gedmo\Loggable\Entity
alias: GedmoLoggable # this one is optional and will default to the name set for the mapping
lobbytrack:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: lobbytrack
mappings:
Lobbytrack:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Lobbytrack'
prefix: 'App\Entity\Lobbytrack'
alias: Lobbytrack
I'm testing with an entity in the lobbytrack database, visitor.
$entityClass = 'App\Entity\Lobbytrack\Visitor';
$this->entityManager = $this->managerRegistry->getManagerForClass($entityClass);
If I inspect the returned entityManager, I see that it's getting the default entity manager, when it should be returning lobbytrack.
The config of the visitor entity puts it in the appropriate namespace, which as far as I can tell, is how it determines which manager matches which entity.
namespace App\Entity\Lobbytrack;
use Doctrine\ORM\Mapping as ORM;
/**
* Visitor
*
* @ORM\Table(name="visitor")
* @ORM\Entity(repositoryClass="App\Repository\Lobbytrack\VisitorRepository")
* @ORM\HasLifecycleCallbacks
*/
class Visitor
{
I'm thinking there is something about my configuration that is breaking this, but I can't see what.
Placing the 'default' entity manager last in doctrine.yaml got this to work as expected.
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
lobbytrack:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: lobbytrack
mappings:
Lobbytrack:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Lobbytrack'
prefix: 'App\Entity\Lobbytrack'
alias: Lobbytrack
vcenter:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: vcenter
mappings:
Vcenter:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Vcenter'
prefix: 'App\Entity\Vcenter'
alias: Vcenter
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
gedmo_loggable:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity'
prefix: Gedmo\Loggable\Entity
alias: GedmoLoggable # this one is optional and will default to the name set for the mapping
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With