1

In my application, there are custom configs and I want to get them into the model.

I read about one way, but it can not perform:

namespace Core\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
use Zend\Db\Sql\Delete,
    Zend\Db\Sql\Insert,
    Zend\Db\Sql\Update,
    Zend\Db\Sql\Select;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class BaseModel extends AbstractTableGateway implements ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator() {
        return $this->serviceLocator;
    }

    public function __construct()
    {
        $this->featureSet = new FeatureSet();
        $this->featureSet->addFeature(new GlobalAdapterFeature());
        $this->initialize();
    }
}

In the model I prescribe

$config = $this->getServiceLocator()->get('config');

or

$config = $this->getServiceLocator();

but the result = NULL

Who can tell what I'm doing wrong?

2
  • what about: $this->getServiceLocator()->get('Configuration'); Commented Nov 21, 2012 at 16:29
  • How are you creating instances of your BaseModel class? If you're not pulling it from the application's pre-configured service manager, the service locator will not be automatically injected into your object. Commented Nov 21, 2012 at 18:56

1 Answer 1

2

You have to create instances of your classes that extend BaseModel using the ServiceManager. If you use new, then you have to set the ServiceManager yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.