我如何使用类元数据?

时间:2014-07-17 13:46:05

标签: php symfony

ContextErrorException: Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in C:\wamp\www\OxiaOpm\app\cache\dev\appDevDebugProjectContainer.php on line 3673 and defined in C:\wamp\www\OxiaOpm\vendor\doctrine\orm\lib\Doctrine\ORM\EntityRepository.php line 68

任何帮助PLZ

这是我的 UserRepository.php

<?php

namespace Oxia\UserBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
#use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;


class UserRepository extends EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $q = $this
            ->createQueryBuilder('u')
            ->where('u.username = :username OR u.email = :email')
            ->setParameter('username', $username)
            ->setParameter('email', $username)
            ->getQuery();

        try {
            // La méthode Query::getSingleResult() lance une exception
            // s'il n'y a pas d'entrée correspondante aux critères
            $user = $q->getSingleResult();
        } catch (NoResultException $e) {
            throw new UsernameNotFoundException(sprintf('Unable to find an active admin     AcmeUserBundle:User object identified by "%s".', $username), 0, $e);
        }

        return $user;
    }

    public function refreshUser(UserInterface $user)
    {
        $class = get_class($user);
        if (!$this->supportsClass($class)) {
            throw new UnsupportedUserException(
                sprintf(
                    'Instances of "%s" are not supported.',
                    $class
                )
            );
        }

        return $this->find($user->getId());
    }

    public function supportsClass($class)
    {
        return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
    }
}

这是我的services.yml

parameters:
    webservice_user_provider.class: Oxia\UserBundle\Entity\UserRepository

services:
    oxia_project.security.my_password_encoder:
        class: Oxia\UserBundle\Security\Encoder\AdminPasswordEncoder

    webservice_user_provider:
        class: %webservice_user_provider.class%
        arguments: [@doctrine.orm.entity_manager]

1 个答案:

答案 0 :(得分:3)

实体存储库的构建应由实体管理器处理。我假设通过将存储库配置为服务,您手动执行此操作。您可以使用工厂来构建服务:

services.yml

my_user_provider:
    class: Oxia\UserBundle\Entity\UserRepository
    factory_service: doctrine.orm.entity_manager
    factory_method: getRepository
    arguments: [Oxia\UserBundle\Entity\User]

对于Symfony&gt; = 2.7,您应该使用以下样式(不推荐使用factory_*配置键):

my_user_provider:
    class: Oxia\UserBundle\Entity\UserRepository
    factory:
        - "@doctrine.orm.entity_manager"
        - getRepository
    arguments: [Oxia\UserBundle\Entity\User]