0
我在我的Symfony2項目中使用JMSDiExtraBundle。Symfony2 JMSDiExtraBundle子服務依賴關係錯誤
這裏是我的問題:
Repository.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,因爲我明確聲明第二個參數需要成爲上傳器實例。
下面是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'));
}
這是一個預期的行爲?或者一個錯誤呢?或者我做錯了什麼?
需要諮詢!
你爲什麼要在抽象類中添加註入?只需刪除抽象父類中的@InjectParams部分,因爲它是抽象的,你不能實例化它。 – m0c
它看起來像一個錯誤,也許你應該報告它? –
@ m0c這是一個非常好的點,但它並不真正解決我對行爲的好奇心。但你的答案拯救了一天:) –