我在 Symfony2 项目中使用 JMSDiExtraBundle。
这是我的问题:
存储库.php
abstract class Repository extends DocumentRepository implements ReadOnlyRepositoryInterface {
protected $dm;
protected $repo;
protected $query;
/**
* @InjectParams({
* "dm" = @Inject("doctrine.odm.mongodb.document_manager")
* })
*/
public function __construct(DocumentManager $dm) {
$this->dm = $dm;
parent::__construct($dm, $this->dm->getUnitOfWork(), new ClassMetadata($this->getDocumentName()));
$this->query = $this->dm->createQueryBuilder($this->getDocumentName());
}
}
PostRepository.php
/**
* @Service("post_repository")
*/
class PostRepository extends Repository implements PostRepositoryInterface {
private $uploader;
/**
* @InjectParams({
* "dm" = @Inject("doctrine.odm.mongodb.document_manager"),
* "uploader" = @Inject("uploader"),
* })
*/
public function __construct(DocumentManager $dm, UploaderService $uploader) {
parent::__construct($dm);
$this->uploader = $uploader;
}
}
可以看出,PostRepository 需要 2 个依赖项:DocumentManager(后来作为父级注入到 Repository)和 Uploader。
但似乎 Symfony 做了一些事情,它假设 PostRepository 需要 3 个依赖项:DocumentManager、DocumentManager(再次)和 Uploader,这当然会出错,因为我明确指出第二个参数必须是 Uploader 实例。
这里来自appDevDebugProjectContainer.xml
:
<service id="post_repository" class="BusinessLounge\BlogBundle\Repository\PostRepository">
<argument type="service" id="doctrine_mongodb.odm.default_document_manager"/>
<argument type="service" id="doctrine_mongodb.odm.default_document_manager"/>
<argument type="service" id="uploader"/>
</service>
和appDevDebugProjectContainer.php
:
/**
* Gets the 'post_repository' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return BusinessLounge\BlogBundle\Repository\PostRepository A BusinessLounge\BlogBundle\Repository\PostRepository instance.
*/
protected function getPostRepositoryService()
{
$a = $this->get('doctrine_mongodb.odm.default_document_manager');
return $this->services['post_repository'] = new \BusinessLounge\BlogBundle\Repository\PostRepository($a, $a, $this->get('uploader'));
}
这是预期的行为吗?或者可能是一个错误?还是我做错了什么?
需要建议!