I am trying to create a method inside a doctrine 2 entity in zend framework. It is just to keep the code DRY. I want to retrieve the user object if they are logged in, and FALSE other wise:
public function getCurrentUserId() {
//returns false if not logged in, user object otherwise
$auth = Zend_Auth::getInstance();
$id = $auth->getidentity();
$user = $this->_em->getRepository('Entities\User')
->findOneByid($id);
if (is_null($user))
return false;
else
return $user;
}
}
This works fine within a controller action, but causes the following error here:
PHP Fatal error: Doctrine\Common\ClassLoader::loadClass(): Failed opening required '/var/www/myswap/application/models/Repositories/Zend_Auth.php'
Why, and how can I avoid this?
I’m going to take a guess and assume you’re using namespaces since it looks like that.
On the line where you use
Zend_Auth, prefix it with a \ – eg.$auth = \Zend_Auth::getInstance();The reason for this is that namespaces in PHP are relative. Thus, if you try to use just
Zend_Authit assumes you want an object in the current namespace. By prefixing it with a \, you’re telling it you wantZend_Authfrom root.I’d suggest familiarizing yourself with the namespaces manual page