2014-04-03 96 views
1

The examples I have seen使用Zend \ Authentication實例化一個指向數據庫的DbAdapter。但我想將它傳遞給我的表格網關模型。這是可能的還是我誤解了一些東西?zend 2:如何將Zend Authentication Adapter DbTable連接到Zend Db TableGateway TableGateway

我有這個安裝在我的Module.php:

'MyModule\Model\UserTable' => function($sm) { 
     $tableGateway = $sm->get('UserTableGateway'); 
     $table = new UserTable($tableGateway); 
     return $table; 
    }, 
    'UserTableGateway' => function($sm) { 
     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
     $resultSetPrototype = new ResultSet(); 
     $resultSetPrototype->setArrayObjectPrototype(
      new User()); 
     return new TableGateway('view_users', $dbAdapter, null, 
      $resultSetPrototype); 
    }, 
), 

現在在控制器動作,我想這樣做:

use Zend\Authentication\Adapter\DbTable as AuthAdapter; 
... 
$authAdapter = new AuthAdapter(**$this->getUserTable()->getAdapter()**); 
$authAdapter 
    ->setTableName('users') 
    ->setIdentityColumn('username') 
    ->setCredentialColumn('password') 

如何集成我TableGateway使用Zend \ Authentication的模型?

回答

0

試試這個 -

首先 - 你應該在配置/自動加載/ global.php下面的代碼(或其他文件)

'service_manager' => array(
    'factories' => array(
     'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', 
    ), 
), 

二 - 控制器的動作 -

$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); 

// create auth adapter 
$authAdapter = new AuthAdapter($dbAdapter); 

// configure auth adapter 
$authAdapter->setTableName('users') 
     ->setIdentityColumn('username') 
     ->setCredentialColumn('password'); 
相關問題