gpt4 book ai didi

symfony - FOSUserBundle:无法识别的字段:usernameCanonical

转载 作者:行者123 更新时间:2023-12-04 21:37:19 25 4
gpt4 key购买 nike

首先,我知道 SO 充满了这样的问题,但我尝试根据这些响应组合不同的配置值,但没有运气。

我将 FOSUserBundle 与我自己的 User 类一起使用,并且在提交登录表单时出现此错误:

Unrecognized field: usernameCanonical



这是我的一些代码:
doctrine:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# mappings:
# FOSUserBundle: ~
fos_user:
service:
mailer: fos_user.mailer.twig_swift
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User

测试的一些变体包括设置 auto_mapping: false和/或取消注释 mappings.FOSUserBundle: ~
这是我的用户类:
<?php

namespace AppBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

use FOS\UserBundle\Model\User as BaseUser;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* AppBundle\Entity\User
*
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User extends BaseUser implements UserInterface
{
const ROLE_DEFAULT = 'ROLE_ADMIN';

/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string", length=100)
*/
protected $name;

/**
* @ORM\Column(type="string", length=40)
* @Assert\Email()
*/
protected $login;

/**
* @ORM\Column(type="string", length=255)
*/
protected $password;

/**
* @ORM\Column(type="string", length=255)
*/
protected $salt;

/**
* @ORM\Column(type="array", length=255)
*/
protected $roles;

/**
* Método requerido por la interfaz UserInterface
*/
public function equals(\Symfony\Component\Security\Core\User\UserInterface $user)
{
return $this->getLogin() == $user->getLogin();
}

/**
* Método requerido por la interfaz UserInterface
*/
public function eraseCredentials()
{
}

/**
* Método requerido por la interfaz UserInterface
*/
public function getUsername()
{
return $this->getLogin();
}

public function __toString()
{
return $this->getName();
}

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

/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

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

/**
* Set login
*
* @param string $login
*/
public function setLogin($login)
{
$this->login = $login;
}

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

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

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

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

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

/**
* Adds a role to the user.
*
* @param string $role
*/
public function addRole($role)
{
$role = strtoupper($role);
if ($role === static::ROLE_DEFAULT) {
return;
}

if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
}

/**
* Returns the user roles
*
* Implements SecurityUserInterface
*
* @return array The roles
*/
public function getRoles()
{
$roles = $this->roles;

foreach ($this->getGroups() as $group) {
$roles = array_merge($roles, $group->getRoles());
}

// we need to make sure to have at least one role
$roles[] = static::ROLE_DEFAULT;

return array_unique($roles);
}

/**
* Set roles
*
* @param string $roles
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
}

/**
* Never use this to check if this user has access to anything!
*
* Use the SecurityContext, or an implementation of AccessDecisionManager
* instead, e.g.
*
* $securityContext->isGranted('ROLE_USER');
*
* @param string $role
* @return Boolean
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->getRoles(), true);
}

}

登录(实际上是 layout.html.twig)模板已被覆盖并且显然呈现正确,我的版本是:
  • Symfy:Symfony 2.8.2 版 - app/dev/debug
  • "friendsofsymfony/user-bundle": "^1.3"
  • console doctrine:schema:update已被执行并且它没有检测到任何更多的变化,尽管数据库表中不存在 usernameCanonical 或电子邮件。

    谢谢

    最佳答案

    使用 FOSUserBundle 1.3.x,您必须扩展 FOS\UserBundle\Entity\User而不是 FOS\UserBundle\Model\User (见 http://symfony.com/doc/1.3.x/bundles/FOSUserBundle/index.html#a-doctrine-orm-user-class)。

    关于symfony - FOSUserBundle:无法识别的字段:usernameCanonical,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34855322/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com