FOS用户包注册“Entity user”类型的实体缺少为字段'ID '分配的ID


FOS user bundle Registration "Entity of type EntityUser is missing an assigned ID for field 'id'

我已经安装了FOS user bundle来管理登录&注册功能,但是当我尝试注册一个新用户时,我收到以下错误:

    Entity of type ****'****'Entity'User is missing an assigned ID for field 'id'.
    The identifier generation strategy for this entity requires the ID field to be
    populated before EntityManager#persist() is called. If you want automatically
    generated identifiers instead you need to adjust the metadata mapping accordingly.

这是我的User.php类:

<?php
// src/Acme/UserBundle/Entity/User.php
namespace  ***'***'Entity;
use FOS'UserBundle'Model'User as BaseUser;
use Doctrine'ORM'Mapping as ORM;
/**
 * @ORM'Entity
 * @ORM'Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM'Id
     * @ORM'Column(type="integer")
     */
    protected $id;

这是我的User.orm.yml:

****'****'Entity'User:
type:  entity
table: fos_user
fields:
    id:
        id:
            type: integer
            generator:
                strategy: AUTO

我知道在持久化新用户的数据时会出现问题,但是我不知道如何让Symfony生成这个id字段。谢谢!

您的yml应该看起来像

type:  entity
table: fos_user
id:
     id:
         type: integer
            generator:
                strategy: AUTO
fields: 

等等…

您的yml配置错误,请尝试:

type:  entity
table: fos_user
fields:
    id:
        id:
            type: integer
            generator:
                strategy: AUTO

我的问题解决了。这是我的User.php类:

<?php
namespace *****'*****'Entity;
use FOS'UserBundle'Model'User as BaseUser;
use Doctrine'ORM'Mapping as ORM;
/**
 * @ORM'Entity
 * @ORM'Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM'Id
     * @ORM'Column(type="integer")
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;

我刚刚删除了所有YAML配置文件,这是注解和YAML之间的冲突问题。现在注册和登录工作正常

相关文章: