开发者

FOS/UserBundle: Unrecognized field: usernameCanonical

I'm very new to Symfony 2.0 and FOS/UserBundle. I'm install it to my project and when I trying to login or register new user I get following errors:

Unrecognized field: usernameCanonical

I haven't no idea what does it mean? I'm put my codes:

app/config/security.yml file

security:
    providers:
        fos_userbundle:
            id: fos_user.user_manager

    firewalls:
        main:
            pattern: .*
            form-login:
                provider: fos_userbundle
                login_path: /login
                use_forward: false
           开发者_C百科     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

User entity located in 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();
    }
}

Please help me?

EDITED: When I trying to register new user I get below errors:

The "App\SecurityBundle\Entity\User" class metadata does not have any "usernameCanonical" field or association mapping.

Added my 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


In my case auto mapping doesn't work because I needed to extend entity (not model)

use FOS\UserBundle\Model\User as BaseUser;

chage to

use FOS\UserBundle\Entity\User as BaseUser;


Maybe Symfony 2 can't read the mapping at all.

At your app/config/config.yml look for the doctrine configuration.

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: ~

That was all I needed.


In my case, in my user class, I was using

use FOS\UserBundle\Document\User as BaseUser;

instead of

use FOS\UserBundle\Entity\User as BaseUser;


Are you trying to use the bundle with Symfony2 Beta1 install? The FriendsOfSymfony UserBundle is developed against the bleeding edge repository of Symfony, and if you're using the beta release from Symfony.com, which is quite outdated compared to the current development commit, undoubtedly things will not work as intended with the UserBundle.

You can either wait for a stable release of both Symfony and the FoSUserBundle, or if you're feeling daring, you can keep your working copy up to date with the Symfony GitHub repository found here, which will allow you to use the FoSUserBundle in the environment for which it is intended.


In my case, I had to upgrade doctrine/orm and doctrine/doctrine-bundle because for some reason the fields in the base User class were not added to the schema.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜