How I can use service Manager in entity class in ZendFramwork2?

199 views Asked by At

How I can use service Manager in entity class in ZendFramwork2? I just can't get it.

Upd: I create entity class of user and create method, which loads additional data about it from DB. I would can load this data via table-class if I have instance of serviceManager. But I can't get this instance.

In the Controller I use this code

public function getNewsTable()
{
    if (!$this->newsTable)
    {
        $sm = $this->getServiceLocator();
        $this->newsTable = $sm->get('Application\Model\NewsTable');
    }
    return $this->newsTable;
}

In the Plugin I use

    public function getServiceManager()
{
    return $this->serviceManager->getServiceLocator();
}
public function setServiceManager(ServiceManager $serviceManager)
{
    $this->serviceManager = $serviceManager;
}

But in the entity class it doesn't work.

1

There are 1 answers

1
GeeH On BEST ANSWER

The entity class needs to impliment "ServiceLocatorAware" interface, and be retrieved by the service locator.