我正在尝试使用 module.config.php 将教义 2 加载到我的服务层对象中:
namespace MyApp;
'di' => array(
'instance' => array(
'alias' => array(
'myapp_doctrine_em' => 'doctrine_em',
),
'orm_driver_chain' => array(
'parameters' => array(
'drivers' => array(
'MyApp' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => __NAMESPACE__ . '\Models\Entity',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Models/Entity'
)
)
)
)
),
'driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => 'MyApp\Models\Entity',
'paths' => array('module/MyApp/src/MyApp/Models/Entity')
),
'MyApp\Controller\DefaultController' => array(
'parameters' => array(
'em' => 'myapp_doctrine_em',
)
),
'MyApp\Models\DefaultModel' => array(
'parameters' => array(
'em' => 'myapp_doctrine_em',
),
),
),
),
如上所示,当我在控制器中实例化该学说时,它确实有效,但是当对我的模型做同样的事情时,它却没有。
问题: - 我如何继续在我的模型中加载理论实体管理器,而不是通过控制器传递它们?
我错过了什么?
谢谢!
添加模型类作为样本
namespace MyApp\Models;
use Doctrine\ORM\EntityManager;
class DefaultModel {
protected $_em;
public function setEntityManager(EntityManager $em) {
$this->_em = $em;
}
public function getEntityManager() {
return $this->_em;
}
}
我的控制器使用依赖注入方法扩展了 DefaultController,例如:
namespace MyApp\Controller;
use Zend\View\Model\ViewModel;
class MyAppController extends DefaultController {
public function indexAction() {
$auth = $this->getDI('MyApp\Models\AuthenticationModel');
$auth->validate($this->getRequest()->post()->toArray());
...
return new ViewModel($data);
}
}
默认控制器:
namespace MyApp\Controller;
use Zend\Mvc\Controller\ActionController,
Zend\View\Model\ViewModel;
class DefaultController extends ActionController {
protected function getDI($service) {
$di = new Zend\Di\Di();
$im = new Zend\Di\InstanceManager();
$di->setInstanceManager($im);
return $di->get($service);
}
}
认证模型:
namespace MyApp\Models;
class AuthenticationModel extends DefaultModel {
public function validate(array $vars) {
...
}
}