本文介绍了ZF2在视图助手中使用数据库表模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要在我的layout.phtml中显示数据库计数,我想使用视图助手来显示该计数(在db字段中设置).
To show an database count in my layout.phtml I want to use the view helper to render ths count (set in an db field).
如何在视图助手中使用数据库模型?
How do I use my database model in a view helper?
助手:
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class CountHelper extends AbstractHelper
{
protected $count;
public function __invoke()
{
return $this->count();
}
public function setTableCount($sm, $myCountTable)
{
$this->count = $sm->get($myCountTable)->getCount();
return $this->count;
}
}
模块
public function getViewHelperConfig()
{
return array(
'factories' => array(
'CountHelper' => function($sm) {
$helper = new \Application\View\Helper\CountHelper();
$helper->setTableCount($sm, 'Application\Model\MyCountTable');
return $helper;
},...
错误:
推荐答案
创建视图助手
namespace My\View\Helper;
use Zend\View\Helper\AbstractHelper;
class CounterHelper extends AbstractHelper
{
protected $count;
public function __invoke()
{
return $this->count;
}
public function setTableCount($sm, $mytablemodel)
{
$this->count = $sm->get($mytablemodel)->getCountedData();
return $this->count;
}
}
并通过工厂注入view_helpers
and inject view_helpers via factories
'view_helpers' => array(
'factories' => array(
'counter_helper' => function($sm) {
$helper = new \My\View\Helper ;
$helper->setTableCount($sm, 'mytablemodelthatalreadyregisteredinSM');
return $helper;
}
)
),
这篇关于ZF2在视图助手中使用数据库表模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!