2

要在我的 layout.phtml 中显示数据库计数,我想使用视图助手来呈现计数(在 db 字段中设置)。

如何在视图助手中使用我的数据库模型?

帮手:

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

错误:

可捕获的致命错误:传递给 Application\Model\MyeCountTable::__construct() 的参数 1 必须是 Zend\Db\TableGateway\TableGateway 的一个实例,没有给出,在 /usr/local/zend/share/ZendFramework2/library/Zend 中调用/ServiceManager/AbstractPluginManager.php 在第 175 行并定义在

4

1 回答 1

1

创建视图助手

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

'view_helpers' => array(
    'factories' => array(
     'counter_helper' => function($sm) {
        $helper = new \My\View\Helper ;
            $helper->setTableCount($sm, 'mytablemodelthatalreadyregisteredinSM');

        return $helper;
     }
    )
),
于 2013-01-07T16:24:04.187 回答