如何使用doctrine 2代替 zend_db 获取auth适配器:
private function getAuthAdapter() {
$authAdapter= new Zend_Auth_Adapter_DbTable(
Zend_Db_Table::getDefaultAdapter()
);
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
return $authAdapter;
}
我的测试遵循教程:
$dbAdapter = Doctrine::getConnectionByTableName('users');
$authAdapter = new Zend_Auth_Adapter_Doctrine_Table( $dbAdapter);
//downloaded from here link //http://framework.zend.com/wiki/download/attachments/3866950/Table.php
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
return $authAdapter;
我得到了
Warning: include_once(Doctrine.php): failed to open stream: No such file or directory in C:\wamp\www\MyProject\library\Zend\Loader.php on line 134
我没有任何类包含getConnectionByTableName !!
-----
C:\wamp\www\MyProject\library\Doctrine>tree
C:.
├───Common
│ ├───Annotations
│ ├───Cache
│ ├───Collections
│ └───Util
├───DBAL
│ ├───Driver
│ │ ├───IBMDB2
│ │ ├───OCI8
│ │ ├───PDOIbm
│ │ ├───PDOMySql
│ │ ├───PDOOracle
│ │ ├───PDOPgSql
│ │ ├───PDOSqlite
│ │ └───PDOSqlsrv
│ ├───Event
│ │ └───Listeners
│ ├───Logging
│ ├───Platforms
│ ├───Schema
│ │ └───Visitor
│ ├───Tools
│ │ └───Console
│ │ ├───Command
│ │ └───Helper
│ └───Types
├───ORM
│ ├───Event
│ ├───Id
│ ├───Internal
│ │ └───Hydration
│ ├───Mapping
│ │ └───Driver
│ ├───Persisters
│ ├───Proxy
│ ├───Query
│ │ ├───AST
│ │ │ └───Functions
│ │ ├───Exec
│ │ └───Expr
│ └───Tools
│ ├───Console
│ │ ├───Command
│ │ │ ├───ClearCache
│ │ │ └───SchemaTool
│ │ └───Helper
│ ├───Event
│ └───Export
│ └───Driver
└───Symfony
└───Component
├───Console
│ ├───Command
│ ├───Helper
│ ├───Input
│ ├───Output
│ └───Tester
└───Yaml
-----------
提前致谢:)
答案 0 :(得分:0)
解决方案:
class AuthenticationController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function loginAction()
{
$this->view->title='Login';
if(Zend_Auth::getInstance()->hasIdentity())
{$this->_redirect();}
$request=$this->getRequest();
$form=new Form_LoginForm();
if($request->isPost())
{if($form->isValid($this->_request->getPost())){
$authAdapter =$this->getAuthAdapter();
$username=$form->getValue('username');
$password=$form->getValue('password');
$authAdapter->setIdentity($username)
->setCredential(md5($password));
$auth=Zend_Auth::getInstance();
$result=$auth->authenticate($authAdapter);
if($result->isValid())
{ $doctrine = $this->getDoctrineContainer();
$em = $doctrine->getEntityManager();
$identity = $em->getRepository('\ZC\Entity\Users')
->find($authAdapter->getIdentity());
$authStorage=$auth->getStorage();
$authStorage->write($identity);
$this->redirect($_SERVER['HTTP_REFERER']);
}else{
$this->view->errorMessage='Username or password is wrong';
}
}
}
$this->view->form=$form;
}
public function logoutAction()
{
Zend_auth::getInstance()->clearIdentity();
$this->_redirect();
}
public function getDoctrineContainer()
{
return $this->getInvokeArg('bootstrap')->getResource('doctrine');
}
private function getAuthAdapter() {
$doctrine = $this->getDoctrineContainer();
$em = $doctrine->getEntityManager();
$authAdapter = new Doctrine\Auth\doctrine2($em);
$authAdapter->getIdentity();
$authAdapter->setEntityClassName('ZC\Entity\users')
->setIdentityField('username')
->setCredentialField('password');
return $authAdapter;
}
}