doctrine-orm - zf2 的学说映射异常 :class "" does not exist?

标签 doctrine-orm zend-framework2

我正在使用 zf2 和doctrine2 创建用户 Controller ,我的注册操作工作正常,但在登录操作中,我收到如下异常:

F:\PHP\EasySurvey\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:96

消息:

Class '' does not exist

堆栈跟踪:

#0 F:\PHP\EasySurvey\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php(43): Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('')
#1 F:\PHP\EasySurvey\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(267): Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getParentClasses('')
#2 F:\PHP\EasySurvey\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(297): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getParentClasses('')
#3 F:\PHP\EasySurvey\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(211): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('')
#4 F:\PHP\EasySurvey\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(295): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('')
#5 F:\PHP\EasySurvey\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php(67): Doctrine\ORM\EntityManager->getClassMetadata('')
#6 F:\PHP\EasySurvey\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php(50): Doctrine\ORM\Repository\DefaultRepositoryFactory->createRepository(Object(Doctrine\ORM\EntityManager), '')
#7 F:\PHP\EasySurvey\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(759): Doctrine\ORM\Repository\DefaultRepositoryFactory->getRepository(Object(Doctrine\ORM\EntityManager), NULL)
#8 F:\PHP\EasySurvey\vendor\doctrine\doctrine-module\src\DoctrineModule\Options\Authentication.php(168): Doctrine\ORM\EntityManager->getRepository(NULL)
#9 F:\PHP\EasySurvey\vendor\doctrine\doctrine-module\src\DoctrineModule\Authentication\Adapter\ObjectRepository.php(134): DoctrineModule\Options\Authentication->getObjectRepository()
#10 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\Authentication\AuthenticationService.php(110): DoctrineModule\Authentication\Adapter\ObjectRepository->authenticate()
#11 F:\PHP\EasySurvey\module\Easysurvey\src\Easysurvey\Controller\UserController.php(105): Zend\Authentication\AuthenticationService->authenticate()
#12 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Easysurvey\Controller\UserController->loginAction()
#13 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#14 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#15 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#18 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#19 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#20 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#21 F:\PHP\EasySurvey\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#22 F:\PHP\EasySurvey\public\index.php(17): Zend\Mvc\Application->run()
#23 {

在我的 Controller 中:

    public function loginAction()
{
    if ($user = $this->identity()) {
       // return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
    }
    $form = new LoginForm();
    $form->get('submit')->setValue('Log in');
    $messages = null;

    $request = $this->getRequest();
    if ($request->isPost()) {

        $form->setInputFilter(new LoginFilter($this->getServiceLocator()));
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $data = $form->getData();
            $authService = $this->getServiceLocator()->get('Zend_Authentication_AuthenticationService');
            $adapter = $authService->getAdapter();

            $usernameOrEmail = $data['usernameOrEmail'];

            // check for email first
            if ($user = $this->getEntityManager()->getRepository('Easysurvey\Entity\User')->findOneBy(array('email' => $usernameOrEmail))) {
                // Set username to the input array in place of the email
                $data['usernameOrEmail'] = $user->getUsername();
            }

            $adapter->setIdentityValue($data['usernameOrEmail']);
            $adapter->setCredentialValue($data['password']);
            $authResult = $authService->authenticate();
            if ($authResult->isValid()) {
                $identity = $authResult->getIdentity();
                $authService->getStorage()->write($identity);
                $time = 1209600; // 14 days (1209600/3600 = 336 hours => 336/24 = 14 days)

                if ($data['rememberme']) {
                    $sessionManager = new \Zend\Session\SessionManager();
                    $sessionManager->rememberMe($time);
                }

                return $this->redirect()->toRoute('user');      
            }
            foreach ($authResult->getMessages() as $message) {
                $messages .= "$message\n";
            }
        }
    }
   /*  $viewModel = new ViewModel();
    $viewModel->setVariables(array('key' => 'value'))
    ->setTerminal(true);

    return $viewModel; */
    return new ViewModel(array(
                            'error' => 'Your authentication credentials are not valid',
                            'form'  => $form,
                            'messages' => $messages,
                        ));
}

在我的 module.php 中:

     public function getServiceConfig()
{

   return array(
        'factories' => array(

            'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                return $serviceManager->get('doctrine.authenticationservice.orm_default');

            },

        )
    );
}

在 module.config.php 中:

    'authentication' => array( // this part is for the Auth adapter from DoctrineModule/Authentication
        'orm_default' => array(
                'object_manager' => 'Doctrine\ORM\EntityManager',
                // object_repository can be used instead of the object_manager key
                'identity_class' => 'Easysurvey\Entity\User', //'Application\Entity\User',
                'identity_property' => 'username', // 'username', // 'email',
                'credential_property' => 'password', // 'password',
                'credential_callable' => function(Entity\User $user, $passwordGiven) {
                    if ($user->getPassword() == Pbkdf2::calc('sha256', $passwordGiven, $user->getSalt(), 10000, 32)) {
                        return true;
                    } else {
                        return false;
                    }
                },
        ),
),

我的用户实体:

namespace Easysurvey\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="username", columns={"UserName"}), @ORM\UniqueConstraint(name="email", columns={"Email"})})
 * @ORM\Entity
 */
class User
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="UserToken", type="string", length=100, nullable=false)
 */
private $usertoken;

/**
 * @var string
 *
 * @ORM\Column(name="UserName", type="string", length=50, nullable=true)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="Email", type="string", length=255, nullable=true)
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(name="DisplayName", type="string", length=50, nullable=true)
 */
private $displayname;

/**
 * @var string
 *
 * @ORM\Column(name="Password", type="string", length=255, nullable=false)
 */
private $password;

/**
 * @var string
 *
 * @ORM\Column(name="Salt", type="string", length=255, nullable=false)
 */
private $salt;

/**
 * @var boolean
 *
 * @ORM\Column(name="ConfirmMail", type="boolean", nullable=false)
 */
private $confirmmail = '0';

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime", nullable=false)
 */
private $createdAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated_at", type="datetime", nullable=false)
 */
private $updatedAt;



/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set usertoken
 *
 * @param string $usertoken
 * @return User
 */
public function setUsertoken($usertoken)
{
    $this->usertoken = $usertoken;

    return $this;
}

/**
 * Get usertoken
 *
 * @return string 
 */
public function getUsertoken()
{
    return $this->usertoken;
}

/**
 * Set username
 *
 * @param string $username
 * @return User
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string 
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set email
 *
 * @param string $email
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set displayname
 *
 * @param string $displayname
 * @return User
 */
public function setDisplayname($displayname)
{
    $this->displayname = $displayname;

    return $this;
}

/**
 * Get displayname
 *
 * @return string 
 */
public function getDisplayname()
{
    return $this->displayname;
}

/**
 * Set password
 *
 * @param string $password
 * @return User
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set salt
 *
 * @param string $salt
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string 
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * Set confirmmail
 *
 * @param boolean $confirmmail
 * @return User
 */
public function setConfirmmail($confirmmail)
{
    $this->confirmmail = $confirmmail;

    return $this;
}

/**
 * Get confirmmail
 *
 * @return boolean 
 */
public function getConfirmmail()
{
    return $this->confirmmail;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return User
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime 
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 * @return User
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

}

最佳答案

从快速 View 来看,属性 DoctrineModule\Options\Authentication::$identityClass 似乎未设置。

这就是您收到空类不存在错误的原因。

关于doctrine-orm - zf2 的学说映射异常 :class "" does not exist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26060665/

相关文章:

zend-framework2 - bin 文件夹中的 Zend Framework 2 "Zend Tool Missing"

mysql - zend_db 查询到 Doctrine 2 querybuilder 的转换

php - Symfony2 在一个表单中同一类的多个实体

php - 从 Doctrine 注释生成 UML 类图的工具

mysql - Doctrine 模式更新总是尝试添加 NOT NULL

php - 我可以通过 Google API v3 访问其他 Google 用户的日历吗

mysql - 如何在 ZF2 中获取 SQL 查询中出现的次数

php - 请求了未知的数据库类型枚举,Doctrine\DBAL\Platforms\MySQL57Platform可能不支持它。 Symfony 4

php - 如何使用 PHPUnit 测试 Zend Framework 2 中的 EventListener

namespaces - ZF2 ZendSkeleton 为什么在默认路由中使用 '__NAMESPACE__' key ?