Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:30:44+00:00 2026-05-27T13:30:44+00:00

I wish to use setter injection on my article entity, but I’m new to

  • 0

I wish to use setter injection on my article entity, but I’m new to this and don’t understand it too well.

My error is:

My\NewsBundle\Entity\Article
Fatal error: Call to a member function addArticle() on a non-object 

This happens here in my article entity:

/** 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: my config.yml has:

# 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: I’m running a ContainerAwareCommand and that consumes a feed; extract from Feed entity:

        $article = $em->getRepository('MyNewsBundle:Article')->findOneBy(array('feed'=>$this->getId(), 'link'=>$item->link()));
        if (!$article) {
            $article = new \My\NewsBundle\Entity\Article();

Any advice on how setter injection works or how to get that service in my entity would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:30:45+00:00Added an answer on May 27, 2026 at 1:30 pm

    Entities are not managed nor created by the service container. Doctrine is responsible for translating database results into entities.

    If you want to inject search service to your Article entity you have to do it manually. As it’s rather an optional dependency (you inject it with a setter) you should also check if it’s not null in your (post|pre)Persist hook.

    Example:

    $article = $em->getRepository('MyNewsBundle:Article')
        ->findOneBy(array('feed'=>$this->getId(), 'link'=>$item->link()));
    
    if (!$article) {
        $article = new \My\NewsBundle\Entity\Article();
    }
    
    $article->setSearch($this->get('search'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to use the iBox plugin but I don't know how to: 1)
I wish to use the Translator classes of Zend framework in my app, but
I wish to use JSON.Net on a brand spankin new Mono class library (say
I have two stored procedures I wish to use in my stored procedure, but
I would like to be able to use Spring using setter injection into Scala
I'm developing an app and I wish use the UIImagePickerController but I wish let
I wish to use QtWebKit to load a url for display, but, that's the
I wish use a uisegmentedcontrol in my app with 3 segments but not with
I wish to use a join across some tables but am having some difficulty
I wish to use Wikipedia api to extract the result of this page: http://en.wikipedia.org/wiki/Special:PrefixIndex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.