Php 使用Zend Framework 2连接到mysql
我正在尝试使用zend framework连接mysql数据库,但它无法连接。 我跟进了zend文档和其他一些关于如何连接mysql的教程,但在这行“$sm=$this->getServiceLocator();”上给了我一个错误 错误消息:Php 使用Zend Framework 2连接到mysql,php,mysql,zend-framework2,Php,Mysql,Zend Framework2,我正在尝试使用zend framework连接mysql数据库,但它无法连接。 我跟进了zend文档和其他一些关于如何连接mysql的教程,但在这行“$sm=$this->getServiceLocator();”上给了我一个错误 错误消息: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Post\Model\PostTable global.php return ar
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;
}
//module/Post/src/Post/Model/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;
}
}
我个人认为,“Post\Model\PostTable”显然没有定义,也没有定义在应该定义的地方
“Post\Model\PostTable”在代码库中的何处定义?这与ZF2的规则相比如何?在Module.php中,应该有“Post\Model\PostTable”注入表
祝你好运:)我看到你更新了你的问题,但“//module/Post/src/Post/Model/PostTable.php”似乎是可疑的。(我假设//是一个打字错误)而Post/src/Post似乎就是问题所在。尝试将代码移动到Post/而不是Module.php中的Post/src/Post。您应该使用“Post\Model\PostTable”注入表。是你干的吗?@dixromos98不,我没有。谢谢:)它现在起作用了。很高兴知道:)。我是否应该将评论作为答案发布,以便您可以接受它作为解决方案?是的,您可以@dixromos98ok发布答案。谢谢。你可以更好地解释这个答案。很抱歉回复太晚。请在此处了解更多信息: