Php Zend Framework 2:视图帮助程序中的数据库连接

Php Zend Framework 2:视图帮助程序中的数据库连接,php,zend-framework2,Php,Zend Framework2,我找到了一些与这个问题相关的其他帖子,但是我没能达到我想要的,所以我决定删除所有内容,并在一些帮助下重新开始 这是我到目前为止的工作,它完成了这项工作,但是数据是以数组的形式硬编码提供的,我需要创建一个数据库连接来获取这些数据 在我的模块课程中,我有: public function getViewHelperConfig() { return array( 'factories' => array( 'liveStreaming' =>

我找到了一些与这个问题相关的其他帖子,但是我没能达到我想要的,所以我决定删除所有内容,并在一些帮助下重新开始

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

在我的模块课程中,我有:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'liveStreaming' => function() {
                return new LiveStreaming();
            },
        ),
    );
}    
这是我在视图帮助器中的代码:

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\LiveStreaming
Application\Model\LiveStreamingTable
(遵循ZF2官方教程的说明),我需要一些帮助来继续下一步,这可能与服务定位器有关。

您似乎就快到了。唯一缺少的是调用
$this->getServiceLocator()的功能(因为
AbstractHelper
不提供此方法)

有两种选择

  • LiveStreamingTable
    直接注入视图辅助对象
  • 插入
    ServiceManager
    本身,并在助手中创建
    LiveStreamingTable
选项1生成视图帮助程序的
LiveStreamingTable
a依赖项(在构造函数中键入提示)

工厂变成:

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
(使其再次成为视图辅助对象的依赖项)

然后,您的工厂将看起来像:

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);
            },
        ),
    );
}  
我个人认为,选项1从依赖项注入(DI)的角度来看更有意义-很明显,
LiveStreamingTable
是创建视图助手所需要的

编辑

确保您已向服务管理器注册了
LiveStreaming\Model\LiveStreamingTable
服务(正如我们在执行
$sm->get('LiveStreaming\Model\LiveStreamingTable')时在上述代码中所要求的那样;


向上投票选择选项1-不要将ServiceLocator作为依赖项,这是不干净的!非常感谢你的回答!第一个选项返回错误:致命错误:未捕获异常“Zend\ServiceManager\exception\ServiceNotFoundException”,消息为“Zend\ServiceManager\ServiceManager::get无法获取或创建LiveStreaming\Model\LiveStreamingTable”的实例…@Stoner
$ServiceManager
正在查找名为
LiveStreaming\Model\LiveStreamingTable
但找不到它。它还需要在
Module.php
中注册。我已经更新了我的答案,包括和例子。
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);
            },
        ),
    );
}  
// 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);
     },

   ),
 );
}