內的多個實體管理器的問題我正在追蹤那幾招http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/cookbook/blending-orm-and-mongodb-odm.html#event-subscriber
,當我到達事件訂閱我不能注射合適的entity manager
,一個名爲$this->dm
在構造函數初始化。的Symfony2/Doctrine2:管理聽衆
據我所知,通過該被加載可通過$em = $eventArgs->getEntityManager();
被檢索的實體所使用的實體管理器然後我需要另一個我以下列方式inject
:
services: postload.listener: class: myVendor\myFooBarBundle\Listener\myEntityListener tags: - { name: doctrine.event_listener, event: postLoad } arguments: - "@doctrine.orm.foobar_entity_manager"
那些是我的實體管理器:
://orm.yml orm: entity_managers: default: connection: default mappings: myVendormyFooBarBundle: prefix: "myVendor\myFooBarBundle\Entity" type: annotation is_bundle: true dir: "Entity" foobar: connection: foobar mappings: myVendormyFooBarBundle: prefix: "myVendor\myFooBarBundle\View" type: annotation is_bundle: true dir: "View"使用上述策略,我得到以下錯誤
當injecting
的
Circular reference detected for service "postload.listener", path: "routing.loader -> routing.db.loader -> doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> postload.listener -> doctrine.orm.fooba_entity_manager -> doctrine.dbal.foobar_connection".
這是myVendor\myFooBarBundle\Listener\myEntityListener
類:
class myFooBarEntityListener { public function __construct(\Doctrine\ORM\EntityManager $em) { $this->em = $em; } public function postLoad(LifecycleEventArgs $eventArgs) { $myEntity = $eventArgs->getEntity(); if($myEntity instanceof \myVendor\myFooBarBundle\Entity\myEntity) { $em = $eventArgs->getEntityManager(); $fooBarReflProp = $em->getClassMetadata('myVendor\myFooBarBundle\Entity\myEntity')->reflClass->getProperty('FooBarEntity'); $fooBarReflProp->setAccessible(true); $fooBarEntity = $this->em->getRepository('myVendor\myFooBarBundle\View\myFooBarEntity')->findOneBy(array('id' => $myEntity->getFooBarEntityId())); $fooBarReflProp->setValue($myEntity, $fooBarEntity); } } }
也避免了circular reference error
我已經試過not
注入foobar entity manager
並通過LifecycleEventArgs $eventArgs
得到它:
class myFooBarEntityListener { public function postLoad(LifecycleEventArgs $eventArgs) { $myEntity = $eventArgs->getEntity(); if($myEntity instanceof \myVendor\myFooBarBundle\Entity\myEntity) { $em = $eventArgs->getEntityManager(); $fooBarReflProp = $em->getClassMetadata('myVendor\myFooBarBundle\Entity\myEntity')->reflClass->getProperty('FooBarEntity'); $fooBarReflProp->setAccessible(true); //NOTICE HOW HERE I SHOULD GET THE PROPER ENTITY MANAGER THROUGH $eventArgs $fooBarEntity = $eventArgs->getEntityManager('foobar')->getRepository('myVendor\myFooBarBundle\View\myFooBarEntity')->findOneBy(array('id' => $myEntity->getFooBarEntityId())); $fooBarReflProp->setValue($myEntity, $fooBarEntity); } } }
這最後落實得來我下面錯誤:
An exception has been thrown during the rendering of a template ("Class myVendor\myFooBarBundle\View\myFooBarEntity is not a valid entity or mapped super class.") in "SonataAdminBundle:CRUD:base_list.html.twig" at line 28.
上述錯誤是由$fooBarEntity = $eventArgs->getEntityManager('foobar')->getRepository('myVendor\myFooBarBundle\View\myFooBarEntity')->findOneBy(array('id' => $myEntity->getFooBarEntityId()));
造成的,因爲當我放置echo'hello'; die();就在該行之前,錯誤不會被拋出,但是當放在行後錯誤被拋出時,hello
不會顯示。該錯誤使我認爲,雖然我明確通過$eventArgs->getEntityManager('foobar')
得到foobar
連接,但它仍然給我default
connection/entity manager
。
爲了仔細檢查myVendor\myFooBarBundle\View\myFooBarEntity
語法我去octrine\ORM\Mapping\Driver\DriverChain
,放在下面的代碼:
if(strpos($className, 'myFooBarEntity')) { echo 'Class: '.$className."\n\n"; foreach ($this->_drivers as $namespace => $driver) { echo 'namespace: '.$namespace."\n"; $bool = strpos($className, $namespace); var_dump($bool); echo "\n\n"; } } die();
這DriverChain代碼給我下面的,這就是爲什麼我認爲「foobar的」連接從來沒有使用過或symfony還有某種錯誤解釋orm.yml
文件,它定義了實體管理器以及要使用的命名空間。
類:的Myvendor \ myFooBarBundle \查看\ myFooBarEntity
命名空間:的Myvendor \ myFooBarBundle \實體 布爾(假)
如果我看上去福爾內myVendor\myFooBarBundle\View\myFooBarEntity
的entity
字我只是覺得@ORM\Entity
爲實體定義和@ORM\OneToMany(targetEntity=.....)
與另一個實體的關係。
我希望有人能幫忙,因爲這讓我發瘋。非常感謝!!
感謝很多答案。我已經嘗試了這一切,注入兩個實體經理,注入服務容器,然後獲得教義服務,注入教義服務,然後獲得兩個實體經理,但我總是得到循環引用錯誤。你知道我還能做什麼?感謝高級:) – user846226 2012-03-05 10:24:58
最後,我看了一下'l3pp4rd DoctrineBehaviours',並根據他的結構創建了一個更高級的偵聽器,並且從'doctrine'服務獲取這兩個連接都沒有問題。 – user846226 2012-03-05 11:35:36
因此,這些答案中的任何一個都可以被標記爲已接受,或者至少可以發佈您的解決方案@ user846226?提前致謝。 – webDEVILopers 2014-09-10 07:47:16