【问题标题】:Zend framework 2: How to get custom config from model?Zend 框架 2:如何从模型中获取自定义配置?
【发布时间】:2012-11-21 10:45:19
【问题描述】:

在我的应用程序中,有自定义配置,我想将它们放入模型中。

我读到了一种方法,但它无法执行:

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();
    }
}

在我规定的模型中

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

$config = $this->getServiceLocator();

但结果 = NULL

谁能告诉我我做错了什么?

【问题讨论】:

  • 怎么样:$this->getServiceLocator()->get('Configuration');
  • 如何创建 BaseModel 类的实例?如果您没有从应用程序的预配置服务管理器中提取它,则服务定位器不会自动注入到您的对象中。

标签: configuration model zend-framework2


【解决方案1】:

您必须使用 ServiceManager 创建扩展 BaseModel 的类的实例。如果你使用new,那么你必须自己设置ServiceManager。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2014-05-02
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多