我希望在我的文章實體上使用setter注入,但我對此並不瞭解,也不太瞭解它。如何在實體上使用setter依賴注入
我的錯誤是:
My\NewsBundle\Entity\Article
Fatal error: Call to a member function addArticle() on a non-object
這發生在我的文章實體:
/** Inject service: search */
protected $search;
public function setSearch(Search $search)
{
$this->search = $search;
}
/** @ORM\PostPersist */
public function postPersist()
{
// wrong: does not have id yet for prePersist; changed to postPersist
//$search = new \My\NewsBundle\Entity\Search();
echo get_class($this->search);
$this->search->addArticle($this);
}
FYI:我config.yml有:
# Parameters
parameters:
search.class: My\NewsBundle\Entity\Search
google.class: My\NewsBundle\Entity\Google
flickr.class: My\NewsBundle\Entity\Flickr
article.class: My\NewsBundle\Entity\Article
# Servers
services:
search:
class: %search.class%
google:
class: %google.class%
flickr:
class: %flickr.class%
article:
class: %article.class%
calls:
- [setSearch, [@search]]
FYI:我運行一個ContainerAwareCommand並使用一個feed;從飼料實體提取:
$article = $em->getRepository('MyNewsBundle:Article')->findOneBy(array('feed'=>$this->getId(), 'link'=>$item->link()));
if (!$article) {
$article = new \My\NewsBundle\Entity\Article();
如何setter注入工作或如何讓我的實體服務的任何意見,將不勝感激。
我建議你重新考慮你如何管理你的代碼。實體類被設計成POPO(管理非常基本的setter/getter功能)。你試圖做的更適合於一個entityManager類型的類。 – Inoryy