Search code examples
phpsymfonydoctrine-ormfosuserbundlefosoauthserverbundle

Doctrine doesn't map fields from FOSUserBundle and FOSOAuthServerBundle


I am using Symfony 2.8, FOSUserBundle 1.3 and FOSOAuthServerBundle 1.5

For all of the class needed to those Bundles to work, I ended up with doctrine not updating my schema properly. I mean it doesn't take into account the fields from the Base Class.

CREATE TABLE oauh2_access_tokens (id INT NOT NULL, client_id INT NOT NULL, user_
id INT DEFAULT NULL, PRIMARY KEY(id));
    CREATE TABLE oauth2_auth_codes (id INT NOT NULL, client_id INT NOT NULL, user_id
 INT DEFAULT NULL, PRIMARY KEY(id));
CREATE TABLE oauth2_clients (id INT NOT NULL, PRIMARY KEY(id));
CREATE TABLE oauth2_refresh_tokens (id INT NOT NULL, client_id INT NOT NULL, use
r_id INT DEFAULT NULL, PRIMARY KEY(id));

Here's my config:

doctrine:
    orm:
        #auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    COMPANYAuthBundle: ~

fos_user:
    db_driver: orm
    firewall_name: api
    user_class: COMPANY\AuthBundle\Entity\User

#FOSOAuthBundle Configuration
fos_oauth_server:
    db_driver: orm
    client_class: COMPANY\AuthBundle\Entity\Client
    access_token_class: COMPANY\AuthBundle\Entity\AccessToken
    refresh_token_class: COMPANY\AuthBundle\Entity\RefreshToken
    auth_code_class: COMPANY\AuthBundle\Entity\AuthCode
    service:
        user_provider: fos_user.user_manager

And here's my class User

<?php

namespace COMPANY\AuthBundle\Entity;

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

/**
 * Utilisateur
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

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

So, yes I did put the right use and not FOS\UserBundle\Model\User as BaseUser;

Same thing for the class of OAuthServerBundle: (I'm just putting one here, they're all following the same pattern)

<?php

namespace COMPANY\AuthBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\OAuthServerBundle\Entity\Client as BaseClient;

/**
 * Client
 *
 * @ORM\Table(name="oauth2_clients")
 * @ORM\Entity(repositoryClass="COMPANY\AuthBundle\Repository\ClientRepository")
 */
class Client extends BaseClient
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


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

    public function __construct()
    {
        parent::__construct();
    }
}

Does anybody have an idea why base class' fields aren't put into my db? Thanks :)


Solution

  • Okay, I found the solution after 10 hours of search...

    And the solution is to not forget to add FOSUserBundle and FOSOAuthServerBundle and all the base class bundles to your mapping.....

    So this should be the config:

    doctrine:
    orm:
        #auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    COMPANYAuthBundle: ~
                    FOSUserBundle: ~
                    FOSOAuthBundle: ~
    

    Also, you can't make a bundle inherit from two bundles if you want to use the routes of each. So, create one bundle for each. Then in each of the Bundle class, add the following function:

    public function getParent()
    {
        return "FOSOAuthServerBundle";  //Or return "FOSUserBundle"; but you can't put both
    }