Zend从登录表中获取外来ID

时间:2011-10-10 16:57:11

标签: zend-framework doctrine

我正在使用Zend和Doctrine使用包含外部ID的表登录到另一个表。我需要获取此ID才能在Doctrine查询(通过控制器)中使用它到数据库,如下所示:

$q = Doctrine_Query::create()
        ->from('Lost_Model_Item i')
        ->where('i.StatID = ?', 'I need the ID here') 
        $result = $q->fetchArray();

我试图这样做:

Zend_Auth::getInstance()->getIdentity()->ID

但似乎行不通。我是Zend的新手,有点迷失在这里。你能帮忙吗?

当我使用doctrine时,我创建了一个适配器,如下所示:

public function authenticate()
   {
    $q = Doctrine_Query::create()
         ->from('Lost_Model_Station u')
      ->where('u.username = ? AND u.password = MD5(?)',
                array($this->username, $this->password)
      );
$result = $q->fetchArray();
if (count($result) == 1) {
  $this->_resultArray = $result[0];
  return new Zend_Auth_Result(
    Zend_Auth_Result::SUCCESS, $this->username, array());
} else {
  return new Zend_Auth_Result(
    Zend_Auth_Result::FAILURE, null, 
      array('Authentication unsuccessful')
  );      
}
  }

2 个答案:

答案 0 :(得分:1)

如何使用getIdentity获取用户名,然后在Lost_Model_ItemLost_Model_Station上使用sql join

$q = Doctrine_Query::create()
    ->from('Lost_Model_Item i')
    ->leftJoin('i.Lost_Model_Station u')
    ->where('u.username = ?', Zend_Auth::getInstance()->getIdentity()) 
$result = $q->fetchArray();

这假定为Lost_Model_Station(在基类中)定义了一个学说“hasOne”关系:

public function setUp()
{
    parent::setUp();
    $this->hasOne('Lost_Model_Item', array(
         'local' => 'StatID',
         'foreign' => 'whatever_id_StatID_relates_to'));
}

答案 1 :(得分:0)

Zend_Auth::getInstance()->getIdentity()->ID

你得到了你在Identity中存储的东西。您需要在要求用户登录的位置发布代码。因为您没有将该ID存储在Zend_Auth使用的会话存储中。基本上就是这样做

 $row  = $this->_adapter->getResultRowObject();
 Zend_Auth::getInstance()->getStorage()->write($row);

其中$ this-> _adapter是

$this->_adapter = new Zend_Auth_Adapter_DbTable();
        $this->_adapter->setTableName('users')
                       ->setIdentityColumn('email')
                       ->setCredentialColumn('password');