在我的layout.phtml中顯示數據庫計數我想使用視圖幫助器來呈現計數(在db字段中設置)。ZF2在視圖幫助器中使用數據庫表模型
如何在視圖助手中使用我的數據庫模型?
助手:
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;
},...
錯誤:
Catchable fatal error: Argument 1 passed to Application\Model\MyeCountTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given, called in /usr/local/zend/share/ZendFramework2/library/Zend/ServiceManager/AbstractPluginManager.php on line 175 and defined in
view_helpers有在看這個答案來自@ Jurian作了-sluiman - > http://stackoverflow.com/questions/14175009/zf2-service-locator/14180345#14180345 – Crisp
感謝我更新了上面的代碼 – directory
在你的'getViewHelperConfig()'把'$ helper-> setTableCount()'行更改爲以下內容,'$ helper-> setTableCount($ sm-> getServiceLocator(),'Application \ Model \ MyCountTable');' – Crisp