duanjianhe1388 2016-11-23 15:22 采纳率: 100%
浏览 38
已采纳

Doctrine不映射FOSUserBundle和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 :)

  • 写回答

1条回答 默认 最新

  • douwen3362 2016-11-24 13:07
    关注

    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 herit from to bundles if you want to use the routes of each, so create two one bundle for each. Then in each of the Buncle class, add the following function:

    public function getParent()
    {
        return "FOSOAuthServerBundle";  //Or return "FOSUserBundle"; but you can't put both
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法