[英]No database connection
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;
...
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);
}
)
);
}
global.php
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;
}
}
我添加了一個foreach,現在可以轉儲或回顯結果:
public function getAcl()
{
$roles = $this->getRolesTable()->fetchAll();
foreach($roles as $role) {
echo $role->role_id;
echo $role->role_name;
echo $role->role_parent;
}
}
但是,我不明白,為什么我不能轉儲整個對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.