Php 没有数据库连接
我的dbAdapter有什么问题?我正在进行此操作,但无法连接到数据库服务器 以下是一些代码片段: Module.phpPhp 没有数据库连接,php,zend-framework2,Php,Zend Framework2,我的dbAdapter有什么问题?我正在进行此操作,但无法连接到数据库服务器 以下是一些代码片段: Module.php use Zend\ModuleManager\ModuleManager; use Zend\Mvc\MvcEvent; use Acl\Model\Roles; use Acl\Model\RolesTable; use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; ...
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\MvcEvent;
use Acl\Model\Roles;
use Acl\Model\RolesTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
...
public function getServiceConfig()
{
return array(
'factories' => array(
'Acl\Model\RolesTable' => function ($sm) {
$tableGateway = $sm->get('RolesTableGateway');
$table = new RolesTable($tableGateway);
return $table;
},
'RolesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Roles());
return new TableGateway('acl_roles', $dbAdapter, null, $resultSetPrototype);
}
)
);
}
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mycms;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'
)
)
);
global.php
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\MvcEvent;
use Acl\Model\Roles;
use Acl\Model\RolesTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
...
public function getServiceConfig()
{
return array(
'factories' => array(
'Acl\Model\RolesTable' => function ($sm) {
$tableGateway = $sm->get('RolesTableGateway');
$table = new RolesTable($tableGateway);
return $table;
},
'RolesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Roles());
return new TableGateway('acl_roles', $dbAdapter, null, $resultSetPrototype);
}
)
);
}
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mycms;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'
)
)
);
您看,我正在与TableGateway合作。如果进行转储,可以看到exchangeArray()函数中的键,但没有值。如果在global.php中删除整个数据库连接,则不会发生任何更改
连接本身是正确的,我可以获取数据并通过mysql连接
有什么想法吗?谢谢你的提示。没有错误被记录,我在mysql中创建了一个新用户,可以从任何主机访问-没有 我将设置一个新的框架,并复制和粘贴模块“相册”。可能是我的代码有错 编辑:我明白了! 这是我发现错误之前的控制器类。转储只提供了一些没有期望值的键:
class AclController extends AbstractActionController
{
protected $rolesTable;
public function getAcl()
{
$roles = $this->getRolesTable()->fetchAll();
echo "<pre>"; var_dump($roles);
}
public function getRolesTable()
{
if (! $this->rolesTable) {
$sm = $this->getServiceLocator();
$this->rolesTable = $sm->get('\Acl\Model\RolesTable');
}
return $this->rolesTable;
}
public function setRolesTable($rolesTable)
{
$this->rolesTable = $rolesTable;
}
}
但是,我不明白,为什么我不能转储整个对象。检查错误日志,您可能会发现有关问题本身的任何提示。