简体   繁体   English

Zend 框架 2:如何从模型中获取自定义配置?

[英]Zend framework 2: How to get custom config from model?

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但结果 = NULL

Who can tell what I'm doing wrong?谁能告诉我我做错了什么?

You have to create instances of your classes that extend BaseModel using the ServiceManager.您必须创建使用 ServiceManager 扩展BaseModel的类的实例。 If you use new , then you have to set the ServiceManager yourself.如果使用new ,则必须自己设置 ServiceManager 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM