Search code examples
phpzend-frameworkzend-dbzend-auth

Zend_Db_Table_Row instance form Zend_Auth


I use Zend_Auth and Zend_Auth_Adapter_DbTable in my project. I want to get a Zend_Db_Table_Row instance from the Zend_Auth adapter (Zend_Auth_Adapter_DbTable). I haven't found any good solution. I need a Zend_Db_Table_Row instance because I use own row class in which I have a method to get data from dependent table.

I know that I can get this data once again but it makes no sense because this data was already fetched from the database by Zend_Auth_Adapter_DbTable.


Solution

  • The trick is putting the row into a Zend_Auth_Storage container. By overriding Zend_Auth_Adapter_Interface::authenticate(), you can do just that.

    /**
     * Authenticate
     *
     * Overriding to provide more information about the authenticated user
     *
     * @return Zend_Auth_Result
     */
    public function authenticate()
    {
        $result = parent::authenticate();
    
        //  Store row on success
        if ($result->getCode() == Zend_Auth_Result::SUCCESS) {
            return new Zend_Auth_Result(
                $result->getCode(),
                $this->getResultRowObject(null, array('*')),
                $result->getMessages()
            );
        } else {
            return $result;
        }
    }