我正在按照那个食谱做http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/cookbook/blending-orm-and-mongodb-odm.html#event-subscriber
当我到达事件订阅者时,我无法注入正确的entity manager
,那个名叫$this->dm
在构造函数中初始化。
据我了解,正在加载的实体使用的实体管理器可以通过检索$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"
When injecting
the foobar entity manager
使用上述策略我得到以下错误:
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".
That's myVendor\myFooBarBundle\Listener\myEntityListener
class:
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
未显示。这个错误让我认为虽然我明确得到了foobar
连接通过$eventArgs->getEntityManager('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\View\myFooBarEntity
命名空间:myVendor\myFooBarBundle\Entity
布尔(假)
如果我寻找entity
里面的话myVendor\myFooBarBundle\View\myFooBarEntity
我刚刚发现@ORM\Entity
对于实体定义以及@ORM\OneToMany( targetEntity=.....)
用于与另一个实体的关系。
我希望有人能提供帮助,因为这让我发疯。多谢!!