Zend Framework 2:视图助手中的数据库连接

时间:2013-11-05 11:28:43

标签: php zend-framework2

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

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

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

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

1 个答案:

答案 0 :(得分:7)

你好像差不多了。唯一缺少的是能够从视图助手中调用$this->getServiceLocator();(因为AbstractHelper不提供此方法)。

有两个选项

  • 直接将LiveStreamingTable注入视图助手
  • 注入ServiceManager本身并在帮助程序中创建LiveStreamingTable

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

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

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

就我个人而言,从依赖注入(DI)的角度来看,我认为选项1 更有意义 - 显然LiveStreamingTable是创建视图助手所需要的。

修改

确保您已向服务管理员注册了LiveStreaming\Model\LiveStreamingTable服务(正如我们在上面的代码中提出$sm->get('LiveStreaming\Model\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);
     },

   ),
 );
}