【问题标题】:The Doctrine repository "Doctrine\ORM\EntityRepository" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterfaceDoctrine 存储库 "Doctrine\ORM\EntityRepository" 必须实现 Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface
【发布时间】:2016-05-05 19:46:54
【问题描述】:

我收到错误The Doctrine repository "Doctrine\ORM\EntityRepository" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.

这主要是在 symfony 自己的登录和数据库以及学说示例之后完成的,并考虑了一些自定义。

客户.php: `

// src/AppBundle/Entity/Customer.php

namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Security\Core\User\UserInterface;
 use Symfony\Component\Security\Core\User\AdvancedUserInterface;

  /**
   * @ORM\Entity(repositoryClass="AppBundle\Entity\CustomerRepository")
   */

 class Customer implements AdvancedUserInterface, \Serializable
 {

 /**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $customer_firstname;

/**
 * @var string
 */
private $customer_lastname;

/**
 * @var string
 */
private $customer_email;

/**
 * @var string
 */
private $customer_password;

/**
 * @var string
 */
private $customer_salt;

/**
 * @var string
 */
private $customer_notes;

/**
 * @var integer
 */
private $customer_server_id;

/**
 * @var string
 */
private $customer_panel_account;

/**
 * @var integer
 */
private $customer_invoicing_account;

/**
 * @var integer
 */
private $customer_is_active;


/**
 * Set id
 *
 * @param integer $id
 *
 * @return Customer
 */
public function setId($id)
{
    $this->id = $id;

    return $this;
}

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

/**
 * Set customerFirstname
 *
 * @param string $customerFirstname
 *
 * @return Customer
 */
public function setCustomerFirstname($customerFirstname)
{
    $this->customer_firstname = $customerFirstname;

    return $this;
}

/**
 * Get customerFirstname
 *
 * @return string
 */
public function getCustomerFirstname()
{
    return $this->customer_firstname;
}

/**
 * Set customerLastname
 *
 * @param string $customerLastname
 *
 * @return Customer
 */
public function setCustomerLastname($customerLastname)
{
    $this->customer_lastname = $customerLastname;

    return $this;
}

/**
 * Get customerLastname
 *
 * @return string
 */
public function getCustomerLastname()
{
    return $this->customer_lastname;
}

/**
 * Set customerEmail
 *
 * @param string $customerEmail
 *
 * @return Customer
 */
public function setCustomerEmail($customerEmail)
{
    $this->customer_email = $customerEmail;

    return $this;
}

/**
 * Get customerEmail
 *
 * @return string
 */
public function getCustomerEmail()
{
    return $this->customer_email;
}

/**
 * Set customerPassword
 *
 * @param string $customerPassword
 *
 * @return Customer
 */
public function setCustomerPassword($customerPassword)
{
    $this->customer_password = $customerPassword;

    return $this;
}

/**
 * Get customerPassword
 *
 * @return string
 */
public function getCustomerPassword()
{
    return $this->customer_password;
}

/**
 * Set customerSalt
 *
 * @param string $customerSalt
 *
 * @return Customer
 */
public function setCustomerSalt($customerSalt)
{
    $this->customer_salt = $customerSalt;

    return $this;
}

/**
 * Get customerSalt
 *
 * @return string
 */
public function getCustomerSalt()
{
    return $this->customer_salt;
}

/**
 * Set customerNotes
 *
 * @param string $customerNotes
 *
 * @return Customer
 */
public function setCustomerNotes($customerNotes)
{
    $this->customer_notes = $customerNotes;

    return $this;
}

/**
 * Get customerNotes
 *
 * @return string
 */
public function getCustomerNotes()
{
    return $this->customer_notes;
}

/**
 * Set customerServerId
 *
 * @param integer $customerServerId
 *
 * @return Customer
 */
public function setCustomerServerId($customerServerId)
{
    $this->customer_server_id = $customerServerId;

    return $this;
}

/**
 * Get customerServerId
 *
 * @return integer
 */
public function getCustomerServerId()
{
    return $this->customer_server_id;
}

/**
 * Set customerPanelAccount
 *
 * @param string $customerPanelAccount
 *
 * @return Customer
 */
public function setCustomerPanelAccount($customerPanelAccount)
{
    $this->customer_panel_account = $customerPanelAccount;

    return $this;
}

/**
 * Get customerPanelAccount
 *
 * @return string
 */
public function getCustomerPanelAccount()
{
    return $this->customer_panel_account;
}

/**
 * Set customerInvoicingAccount
 *
 * @param integer $customerInvoicingAccount
 *
 * @return Customer
 */
public function setCustomerInvoicingAccount($customerInvoicingAccount)
{
    $this->customer_invoicing_account = $customerInvoicingAccount;

    return $this;
}

/**
 * Get customerInvoicingAccount
 *
 * @return integer
 */
public function getCustomerInvoicingAccount()
{
    return $this->customer_invoicing_account;
}

public function isAccountNonExpired()
{
  return true;
}

public function isAccountNonLocked()
{
  return true;
}

public function isCredentialsNonExpired()
{
  return true;
}

/**
 * Check if customer accoutn is activate
 *
 */

 public function isEnabled()
 {
   return $this->customer_is_active;
 }

/**
 * Set customerIsActive
 *
 * @param integer $customerIsActive
 *
 * @return Customer
 */
public function setCustomerIsActive($customerIsActive)
{
    $this->customer_is_active = $customerIsActive;

    return $this;
}

/**
 * Get customerIsActive
 *
 * @return integer
 */
public function getCustomerIsActive()
{
    return $this->customer_is_active;
}

/** Added these functions because of the security controller */

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

 public function getPassword()
 {
   return $this->customer_password;
 }

 public function getSalt()
 {
   return $this->customer_salt;
 }

 public function getRoles()
 {
   return array('ROLE_USER');
 }

 public function eraseCredentials()
 {

 }

 public function serialize()
 {
    return serialize(array(
      $this->id,
      $this->customer_email,
      $this->customer_password,
      $this->customer_is_active
    ));
 }

 public function unserialize($serialized)
 {
   list (
    $this->id,
    $this->customer_email,
    $this->customer_password,
    $this->customer_is_active
   ) = unserialize($serialized);
 }
}

CustomerRepository.php:

<?php

 //src/AppBundle/Entity/CustomerRepository.php

use Symfony\Bridge\Doctrine\Security\User\UserloadInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\ORM\EntityRepository;

class CustomerRepository extends EntityRepository implements   UserLoaderInterface
{

public function loadUserByUsername($email)
  {
  $user = $this->createQueryBuilder('c')
    ->where('c.customer_email = :email')
    ->setParameter('email', $email)
    ->getQuery()
    ->getOneOrNullResult();

    if(null === $user)
    {
      $message = sprintf(
        'Unable to find an active user AppBundle:User object identified by "%s".',
        $email
      );
      throw new UsernameNotFoundException($message);

    }

    return $user;
}
}

我在去年发现了一个类似的问题(堆栈溢出),认为它适用于 2.5* 版本,但对我没有帮助。我正在使用 symfony3。问题链接:The Doctrine repository "Doctrine\ORM\EntityRepository" must implement UserProviderInterface in symfony2

【问题讨论】:

  • 尝试验证存储库是否属于该实体。查看EntityUserProvider,它以与从控制器相同的方式获取存储库。尝试测试一下。
  • 您不能混合使用注释和 yaml/xml 映射,这意味着正在检索默认存储库类而不是您的自定义类。将存储库类行添加到 Resources/config/doctrine 下的映射文件。
  • 我无法通过快速搜索找到如何在 yml 中添加存储库类。我有 Customer.orm.yml 文件,我需要删除 /** * @ORM\Entity(repositoryClass="AppBundle\Entity\CustomerRepository") */ 并添加到 yml 吗?
  • 我找到了如何将存储库类添加到 yml 但现在出现错误:The autoloader expected class "AppBundle\Entity\CustomerRepository" to be defined in file "/Users/Mika/Documents/HTML Projectit/tietorauta secure/src/AppBundle/Entity/CustomerRepository.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
  • 我缺少命名空间 AppBundle\Entity;从存储库文件。 Cerad,您能否发布您的答案,我可以接受,这是“简单”的错误错误,您不能混合注释和 xml/yml 映射:)

标签: php orm doctrine-orm symfony


【解决方案1】:

您不能混合使用注释和 yaml/xml 映射,这意味着正在检索默认存储库类而不是您的自定义类。将存储库类行添加到 Resources/config/doctrine 下的映射文件。

# customer.orm.yml
AppBundle\Entity\Game:
  type:  entity
  table: customers
  repositoryClass: AppBundle\Entity\CustomerRepository

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 2021-04-22
    • 1970-01-01
    • 2018-05-26
    • 2019-07-13
    • 1970-01-01
    • 2019-03-28
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多