2

私のlayout.phtmlにデータベースカウントを表示するには、ビューヘルパーを使用してthsカウント(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 のインスタンスである必要があります。 /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 に答える