Doctrine doesn't map fields from FOSUserBundle User class
Asked Answered
D

4

5

I am using Symfony 2.1 RC1 and the FOSUserbundle on a Windows server running PHP 5.3.13.

I have followed the instructions here but Doctrine doesn't create fields in the database for the properties inherited from the base FOS User class (only the fields from my class).

Trying to login using the FOS login form produces the error:

Unrecognized field: usernameCanonical

I have the following Doctrine configuration:

# 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%
        auto_mapping: true

And the FOSUserBundle config looks like:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: SP\PickList\UserBundle\Entity\User

My User entity:

namespace SP\PickList\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Document\User as BaseUser;

/**
 * SP\PickList\UserBundle\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;


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

Thanks for any help,

James Bench

Droopy answered 21/8, 2012 at 15:59 Comment(0)
U
17

It seems you are mixing instructions for ORM and ODM, if you use Doctrine ORM as per your config, your User class must extend FOS\UserBundle\Entity\User, try to change the use statement as

use FOS\UserBundle\Entity\User as BaseUser;
Unilingual answered 21/8, 2012 at 19:55 Comment(4)
FOS\UserBundle\Entity\User is deprecated in newer versions of FOS UserBundle. You are supposed to extend FOS\UserBundle\Model\User.Chiffchaff
FYI, if you use version 1.3, extend FOS\UserBundle\Entity\User. If you use version 2.0, extend FOS\UserBundle\Model\User.Factious
Thanks you so much !! Spent hours figuring this out and it ended up I was using FOS\User\UserBundle\Model\User !!! :(Punke
I am using 2.0 and I am using "use FOS\UserBundle\Model\User as BaseUser;" and I have a plain entity with just the ID field.. copied and pasted from their getting started..and still I only have the ID column in my DB..any idea why?Sordello
T
5

To save someone some time. Don't forget if you are not using auto_mapping in your ORM config in config.yml you need to add 'FOSUserBundle: ~' to the mapping.

Tamathatamaulipas answered 13/4, 2013 at 18:8 Comment(0)
D
1

Also you must specify which entity user class you'll use in app/config/config.yml

# FOS
fos-user: 
    db_driver: orm
    firewall_name: main #optional
    user_class: FOS\UserBundle\Entity\User
Detrusion answered 28/5, 2013 at 6:20 Comment(0)
O
0

I am using v2.1.2 of the fosuserbundle, my User class is

use FOS\UserBundle\Model\User as BaseUser;
class Nutzer extends BaseUser {...}

and my config.yml looks like

orm:
    default_entity_manager: default
    entity_managers:
        default:
            connection: postgres
            naming_strategy: doctrine.orm.naming_strategy.underscore
            mappings:
                AppBundle: ~ 
        my:
            connection: mysql
            naming_strategy: doctrine.orm.naming_strategy.underscore
            mappings:
                AppBundle: ~
                FOSUserBundle: ~ #this line does not change anything if left out

        ms:
            connection: mssql
            naming_strategy: doctrine.orm.naming_strategy.underscore
            mappings:
               AppBundle: ~

Still only this SQL is created (when creating the schema with --em=my):

CREATE TABLE nutzer (id INT AUTO_INCREMENT NOT NULL, supervisor_id INT DEFAULT NULL, api_key VARCHAR(64) DEFAULT NULL, INDEX IDX_A9B4F58919E9AC5F (supervisor_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;

So all the base fields are missing!

The line in mappings FOSUserBundle: ~ does not change anything.

But for the postgresql and mssql entitymanager everything works!

E.g. for postgres:

CREATE TABLE nutzer (id INT NOT NULL, supervisor_id INT DEFAULT NULL, username VARCHAR(180) NOT NULL, username_canonical VARCHAR(180) NOT NULL, email VARCHAR(180) NOT NULL, email_canonical VARCHAR(180) NOT NULL, enabled BOOLEAN NOT NULL, salt VARCHAR(255) DEFAULT NULL, password VARCHAR(255) NOT NULL, last_login TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, confirmation_token VARCHAR(180) DEFAULT NULL, password_requested_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, roles TEXT NOT NULL, api_key VARCHAR(64) DEFAULT NULL, PRIMARY KEY(id))'
Otter answered 10/10, 2018 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.