我尝试使用zend框架连接到mysql数据库,但它无法连接。 我跟进了zend文档以及一些关于如何连接到mysql的其他教程,但它在这一行上给了我一个错误" $ sm = $ this-> getServiceLocator();&# 34;
错误讯息:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Post\Model\PostTable
global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zend;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),);
local.php
return array(
'db' => array(
'username' => 'root',
'password' => '',
),
);
PostController.php
public function getPostTable() {
if (!$this->postTable) {
$sm = $this->getServiceLocator();
$this->postTable = $sm->get('Post\Model\PostTable');
}
return $this->postTable;
}
//模块/后/ SRC /后/型号/ PostTable.php
<?php
namespace Post\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
class PostTable extends AbstractTableGateway
{
protected $table ='tbl_post';
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
$this->resultSetPrototype = new ResultSet();
$this->resultSetPrototype->setArrayObjectPrototype(new Post());
$this->initialize();
}
public function fetchAll()
{
$resultSet = $this->select();
return $resultSet;
}
}
答案 0 :(得分:0)
我个人认为,很明显“Post \ Model \ PostTable”根本没有定义,或者它应该定义在哪里。
您的代码库中定义的'Post \ Model \ PostTable'在哪里?这与ZF2的规则相比如何?
答案 1 :(得分:0)
在你的Module.php中你应该有&#39; Post \ Model \ PostTable&#39;注入表
祝你好运:)