doujiu3768 2018-07-10 05:50
浏览 49
已采纳

SonataUserBundle + FOSUserBundle注释问题Doctrine主键错误

Using Symfony 4.1, Sonata User Bundle 4.x, and FOSUserBundle 2.1.2.

I am trying to override the table names for the User and Group tables. I therefore added annotations to the auto generated user and group classes:

use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;
use Doctrine\ORM\Mapping as ORM;

/**
 * This file has been generated by the SonataEasyExtendsBundle.
 * @ORM\Entity()
 * @ORM\Table(name="aegis_group")
 * @link https://sonata-project.org/easy-extends 
 * References:
 * @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 */
class Group extends BaseGroup
{
    /**     
     * @ORM\Id      
     * @var int $id
     */
    protected $id;

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

I then modified doctrine.yaml to factor in these annotations:

        mappings:                    
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: App
                FOSUserBundle: ~                                          
                ApplicationSonataUserBundle: 
                    type: annotation
                SonataUserBundle: ~   

However, when I run migrations, doctrine gives me an error:

In MappingException.php line 46:

No identifier/primary key specified for Entity "App\Application\Sonata\User Bundle\Entity\Group" sub class of "Sonata\UserBundle\Entity\BaseGroup". Every Entity must have an identifier/primary key.

How to fix this issue, so that I can use my own custom table names ? All I want to do is to change the database table names, this should not be this involved.

  • 写回答

2条回答 默认 最新

  • duanmi8349 2018-07-14 04:13
    关注

    Well, turns out an XML orm file is generated by the SonataEasyExtendsBundle, in directory Application\Sonata\UserBundle\Resources\config\doctrine folder. One should modify this file (User.orm.xml) to make changes to the table config.

    <?xml version="1.0" encoding="UTF-8"?>
    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    
        <entity name="App\Application\Sonata\UserBundle\Entity\User" table="aegis_user">
    
            <id name="id" column="id" type="integer">
                <generator strategy="AUTO" />
            </id>
    
        </entity>
    
    </doctrine-mapping>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多