我是Symfony 2.0和FOS / UserBundle的新手。我将它安装到我的项目中,当我尝试登录或注册新用户时,我得到以下错误:
无法识别的字段:usernameCanonical
我不知道这是什么意思?我把我的代码放在了:
app / config / security.yml文件
security:
providers:
fos_userbundle:
id: fos_user.user_manager
firewalls:
main:
pattern: .*
form-login:
provider: fos_userbundle
login_path: /login
use_forward: false
check_path: /login_check
failure_path: /not_found
logout: true
anonymous: true
access_control:
# The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
- { path: ^/_wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/_profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
# AsseticBundle paths used when using the controller for assets
- { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
# URL of FOSUserBundle which need to be available to anonymous users
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
- { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Secured part of the site
# This config requires being logged for the whole site and having the admin role for the admin part.
# Change these rules to adapt them to your needs
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/.*, role: ROLE_USER }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPERADMIN: ROLE_ADMIN
位于SecurityBundle中的用户实体:
<?php
namespace App\SecurityBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* App\SecurityBundle\Entity\User
*
* @orm:Table(name="fos_user")
* @orm:Entity
*/
class User extends BaseUser
{
/**
* @var integer $id
*
* @orm:Column(name="id", type="integer")
* @orm:Id
* @orm:GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @orm:Column(type="string", name="username", length="70")
*/
protected $username;
/**
* @orm:Column(type="string", name="password", length="70")
*/
protected $password;
/**
* @orm:Column(type="string", name="email", length="70")
*/
protected $email;
public function __construct()
{
parent::__construct();
}
}
请帮帮我?
已编辑:当我尝试注册新用户时,我会收到以下错误:
The "App\SecurityBundle\Entity\User" class metadata does not have any "usernameCanonical" field or association mapping.
添加了我的config.yml:
imports:
- { resource: parameters.ini }
- { resource: security.yml }
framework:
secret: %secret%
charset: UTF-8
error_handler: null
csrf_protection:
enabled: true
router: { resource: "%kernel.root_dir%/config/routing.yml" }
validation: { enabled: true, annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: %locale%
lifetime: 3600
auto_start: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
filters:
cssrewrite: ~
# closure:
# jar: %kernel.root_dir%/java/compiler.jar
# yui_css:
# jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar
fos_user:
db_driver: orm
firewall_name: main
class:
model:
user: App\SecurityBundle\Entity\User
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
dbname: %database_name%
user: %database_user%
password: %database_password%
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
mappings:
FOSUserBundle: ~
AppSecurityBundle: { type: annotation, dir: Entity/ }
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
jms_security_extra:
secure_controllers: true
secure_all_services: false
答案 0 :(得分:20)
也许Symfony 2根本无法读取映射。
在你的app / config / config.yml上寻找教条配置。
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
mappings:
ApplicationSonataUserBundle: ~
SonataUserBundle: ~
FOSUserBundle: ~
这就是我所需要的一切。
答案 1 :(得分:20)
在我的情况下,自动映射不起作用,因为我需要扩展实体(而不是模型)
use FOS\UserBundle\Model\User as BaseUser;
chage to
use FOS\UserBundle\Entity\User as BaseUser;
答案 2 :(得分:5)
就我而言,在我的用户类中,我正在使用
use FOS\UserBundle\Document\User as BaseUser;
而不是
use FOS\UserBundle\Entity\User as BaseUser;
答案 3 :(得分:1)
您是否尝试使用Symfony2 Beta1安装捆绑包? FriendsOfSymfony UserBundle是针对Symfony的前沿存储库开发的,如果你使用Symfony.com的测试版,与当前的开发提交相比已经过时了,毫无疑问,事情将无法按照UserBundle的预期工作
您可以等待Symfony和FoSUserBundle的稳定版本,或者如果您感觉大胆,可以使用here找到的Symfony GitHub存储库保持您的工作副本最新,这将允许您可以在预期的环境中使用FoSUserBundle。
答案 4 :(得分:0)
在我的情况下,我不得不升级doctrine / orm和doctrine / doctrine-bundle,因为由于某种原因,基本User类中的字段没有添加到模式中。