开发者

Zend get foreign ID from login table

I am using Zend and Doctrine to login using a table containing also a foreign ID to another table. I need to get this ID in order to use it in a Doctrine query (through the controller) to the database like this:

$q = Doctrine_Query::create()
  开发者_如何学运维      ->from('Lost_Model_Item i')
        ->where('i.StatID = ?', 'I need the ID here') 
        $result = $q->fetchArray();

I have tried to get it like this:

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

But it seems to not work. I am new to Zend and a bit lost here. Could you please help?

As I ma working with doctrine I have created an adapter as follow:

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')
  );      
}
  }


How about using getIdentity to get the username and then use a sql join on Lost_Model_Item and Lost_Model_Station

$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();

This assumes that there is a doctrine "hasOne" relation defined for Lost_Model_Station (in the base class):

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


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

well you got what you store in Identity . You need to post the code where you ask user to login . Because there you are not storing the ID inside the session storage which Zend_Auth uses . Basically do this there

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

where $this->_adapter is

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜