2011-05-14 85 views
11

我對Symfony 2.0和FOS/UserBundle非常陌生。我將它安裝到我的項目,當我試圖登錄或註冊新用戶,我得到以下錯誤:FOS/UserBundle:無法識別的字段:usernameCanonical

無法識別的領域:usernameCanonical

我沒有不知道這是什麼意思?我把我的代碼:

應用程序/配置/ security.yml文件位於SecurityBundle

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 

用戶實體:

<?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

用戶ORM映射的所有元數據均以XML格式提供(FOS/UserBundle/Resources/config/doctrine/User.orm。xml),並且您的ORM元數據在註釋中提供。我不認爲你可以在同一個包中使用兩個元數據格式。你發佈這個問題後你有沒有找到解決方案? – Sewdn

+0

是的,我找到了解決方案。 – Zeck

+3

你介意分享解決方案嗎?我正在運行symfony和fosuserbundle的最新版本,並且出現同樣的錯誤。 – jfgrissom

回答

1

你試圖使用與Symfony2的Beta1的捆安裝? FriendsOfSymfony UserBundle是針對Symfony的最新版本庫而開發的,如果您使用的版本是Symfony.com,與目前的開發提交相比已經過時了,無疑這些工具無法像UserBundle那樣工作。

您可以等待Symfony和FoSUserBundle的穩定版本,或者如果您感覺非常勇敢,可以使用Symfony GitHub存儲庫here來更新工作副本,以便您可以使用它FoSUserBundle在它所針對的環境中。

4

對我來說,在我的用戶類,我用

use FOS\UserBundle\Document\User as BaseUser; 

,而不是

use FOS\UserBundle\Entity\User as BaseUser; 
20

也許Symfony的2無法讀取映射的。

在你的app/config/config.yml中查找doctrine配置。

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

+1映射的好處!它開始發生在我停止使用auto_mapping的那一刻;) –

+3

做完這些之後,我更新了模式:php app/console doctrine:schema:update --force並重新創建了admin用戶:php app/console fos:user:create admintest admin @ test.com傳遞 - 超級管理員。那對我來說工作得很好。 – Beniston

20

在我的情況下自動映射不起作用,因爲我需要擴展實體(不模型)

use FOS\UserBundle\Model\User as BaseUser; 

CHAGE到

use FOS\UserBundle\Entity\User as BaseUser; 
+1

使用實體現在[**棄用**](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/User.php#L23),你不應該依賴它。 – Touki

+2

是的,不!如果您安裝了版本「1.3.5」,則此答案正確。如果你在-dev分支,那麼@Touki是對的。 – jakabadambalazs

+1

模型在版本2.0中使用,而實體在1.3中使用 –

0

就我而言,我不得不升級學說/ ORM和doctrine/doctrine-bundle,因爲基於某些原因,User基類中的字段沒有添加到模式中。

相關問題