zend 2:如何将Zend \ Authentication \ Adapter \ DbTable连接到Zend \ Db \ TableGateway \ TableGateway

时间:2014-04-03 20:04:44

标签: php oop zend-framework2

The examples I have seen使用Zend \ Authentication实例化指向数据库的DbAdapter。但我想把它传递给我的表网关模型。这有可能还是我误解了什么?

我在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集成?

1 个答案:

答案 0 :(得分:0)

试试这个 -

首先 - 你应该在config / autoload / 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');