gpt4 book ai didi

php - Zend 框架 2 : Database connection in view helper

转载 作者:可可西里 更新时间:2023-11-01 00:21:36 26 4
gpt4 key购买 nike

我发现了一些与此问题相关的其他帖子,但是我无法实现我想要的,所以我决定删除所有内容并在一些帮助下重新开始...

到目前为止,这是我的工作,它完成了工作,但数据是在数组中硬编码提供的,我需要创建一个数据库连接来获取这些数据。

在我的模块类中我有:

public function getViewHelperConfig()
{
return array(
'factories' => array(
'liveStreaming' => function() {
return new LiveStreaming();
},
),
);
}

这是我在 View 助手中的代码:

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class LiveStreaming extends AbstractHelper
{

protected $liveStreamingTable;

public function __invoke()
{
$events = array(
'1' => array('name' => 'Event name',
'sport' => 'Soccer',
'time' => '11:30'),
'2' => array('name' => 'Event name',
'sport' => 'Soccer',
'time' => '17:00'),
);
return $events;
//this is what should be used (or something like that) to get the data from the db...
//return array('events' => $this->getLiveStreamingTable()->fetchAll() );
}

public function getLiveStreamingTable()
{
if (!$this->liveStreamingTable) {
$sm = $this->getServiceLocator();
$this->liveStreamingTable = $sm->get('LiveStreaming\Model\LiveStreamingTable');
}
return $this->liveStreamingTable;
}
}

所以,我想从数据库中获取$events 数组。我已经创建了 Application\Model\LiveStreamingApplication\Model\LiveStreamingTable(按照 ZF2 官方教程的说明),我需要一些帮助才能进行下一步,这可能与服务定位器有关。

最佳答案

你似乎快到了。唯一缺少的是从 View 助手中调用 $this->getServiceLocator(); 的能力(因为 AbstractHelper 不提供此方法)。

有两种选择

  • LiveStreamingTable 直接注入(inject) View 助手
  • 注入(inject) ServiceManager 本身并在助手中创建 LiveStreamingTable

选项 1 使 LiveStreamingTable 成为 View 助手的依赖项(在构造函数中键入提示)

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use LiveStreaming\Model\LiveStreamingTable;


class LiveStreaming extends AbstractHelper
{
protected $liveStreamingTable;

public function __construct(LiveStreamingTable $liveStreamingTable)
{
$this->liveStreamingTable = $liveStreamingTable;
}

public function getLiveStreamingTable()
{
return $this->liveStreamingTable;
}

}

然后工厂变成:

public function getViewHelperConfig()
{
return array(
'factories' => array(
'liveStreaming' => function($sl) {
// Get the shared service manager instance
$sm = $sl->getServiceLocator();
$liveStreamingTable = $sm->get('LiveStreaming\Model\LiveStreamingTable');
// Now inject it into the view helper constructor
return new LiveStreaming($liveStreamingTable);
},
),
);
}

选项 2 - 实现 ServiceLocatorAwareInterface(使其再次成为 View 助手的依赖项)

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class LiveStreaming extends AbstractHelper implements ServiceLocatorAwareInterface
{
protected $serviceLocator;

protected $liveStreamingTable;

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

public function setServiceLocator(ServiceLocatorInterface $serviceLocator);

public function getServiceLocator();

public function getLiveStreamingTable()
{
if (null == $this->liveStreamingTable) {
$this->liveStreamingTable = $this->getServiceLocator()->get('LiveStreaming\Model\LiveStreamingTable');
}
return $this->liveStreamingTable;
}

}

您的工厂将如下所示:

public function getViewHelperConfig()
{
return array(
'factories' => array(
'liveStreaming' => function($sl) {
// Get the shared service manager instance
$sm = $sl->getServiceLocator();
// Now inject it into the view helper constructor
return new LiveStreaming($sm);
},
),
);
}

就个人而言,我觉得从依赖注入(inject) (DI) 的角度来看,选项 1 更有意义 - 很明显,LiveStreamingTable 是创建查看助手。

编辑

确保你有 LiveStreaming\Model\LiveStreamingTable 服务也注册了服务管理器(正如我们在上面的代码中请求的那样 $sm->get('LiveStreaming\模型\LiveStreamingTable');)

// Module.php
public function getServiceConfig()
{
return array(
'factories' => array(

'LiveStreaming\Model\LiveStreamingTable' => function($sm) {

// If you have any dependencies for the this instance
// Such as the database adapter etc either create them here
// or request it from the service manager
// for example:
$foo = $sm->get('Some/Other/Registered/Service');
$bar = new /Directly/Created/Instance/Bar();

return new \LiveStreaming\Model\LiveStreamingTable($foo, $bar);
},

),
);
}

关于php - Zend 框架 2 : Database connection in view helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com