看進學說EventManager,特別是postLoad生命週期事件,這是由eventmanager進行每一個實體從數據庫加載時間發射。
要將它全部整合到ZF2中,您需要做幾件事情。
首先,寫一個學說味的事件偵聽器:
<?php
class InjectStuffListener {
private $sl;
public function __construct($serviceLocator){
$this->sl = $serviceLocator;
}
public function postLoad($eventArgs){
$entity = $eventArgs->getEntity;
$entity->setThingToBeInjected($this->sl->get('some.thing'));
}
}
然後,像一些Module.php某個地方(也許還有比onBootstrap一個更好的地方,但不管):
<?php
public function onBootstrap(){
$sm = $e->getApplication()->getServiceManager();
$em = $sm->get('doctrine.entitymanager.orm_default');
$dem = $em->getEventManager();
$dem->addEventListener(array(\Doctrine\ORM\Events::postLoad), new InjectStuffListener($sm));
}
什麼你想要注入嗎?我無法想象一個將某些東西注入實體的情況。我確信有一種方法可以做到你想要的東西符合這個概念。 –
在此期間,我採取了另一個方向。我想委託給實體本身的事物現在被委託給管理這些實體的服務。因此,不需要再向實體注入某些東西 – griesi