本文介绍了SonataUserBundle + FOSUserBundle注释问题教义主键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Symfony 4.1,Sonata User Bundle 4.x和FOSUserBundle 2.1.2。

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

我试图覆盖User和Group表的表名。因此,我在自动生成的用户和组类中添加了注释:

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;
    }
}

然后我修改了doctrine.yaml以将这些注释包括在内:

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:

没有标识符/主变量为
Sonata\UserBundle\Enti子类的实体
App\Application\Sonata\User Bundle\Entity\Group子类指定的密钥ty\BaseGroup。每个实体都必须具有
标识符/主键。

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.

如何解决此问题,以便我可以使用我自己的自定义表名称?我要做的就是更改数据库表名,这不应该涉及到。

推荐答案

,结果表明SonataEasyExtendsBundle在目录Application\Sonata\UserBundle\Resources\config\doctrine文件夹中生成了一个XML orm文件。应该修改此文件(User.orm.xml)以更改表配置。

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>

这篇关于SonataUserBundle + FOSUserBundle注释问题教义主键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 02:06