我是 Zend 2 的新手。我制作了一个控制器和模型。
我收到以下错误:
致命错误:在第 25 行的 C:\websites\zend2\module\Pages\src\Pages\Model\PagesTable.php 中的非对象上调用成员函数 get()
我做错了什么?!?!
解决方案:
控制器:
namespace Pages\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController {
protected $pagesTable;
function indexAction() {
return new ViewModel(array(
'pages' => $this->getPagesTable()->fetchAll(),
));
}
public function getPagesTable()
{
if (!$this->pagesTable) {
$sm = $this->getServiceLocator();
$this->pagesTable = $sm->get('Pages\Model\PagesTable');
}
return $this->pagesTable;
}
}
模型:
namespace Pages\Model;
use Zend\Db\TableGateway\TableGateway;
class PagesTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
}
添加模块.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Pages\Model\PagesTable' => function($sm) {
$tableGateway = $sm->get('PagesTableGateway');
$table = new PagesTable($tableGateway);
return $table;
},
'PagesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
return new TableGateway('pages', $dbAdapter, null, $resultSetPrototype);
},
),
);
}