简体   繁体   English

主义Symfony ORM无法使用PHP 7.2生成MySQL UUID

[英]Doctrine Symfony ORM not generating MySQL UUID with PHP 7.2

I cannot get ORM annotations to work so that the UUID is auto-generated in MySQL after the new entity added to the table is flushed. 我无法使ORM批注起作用,以便在刷新添加到表中的新实体后在MySQL中自动生成UUID。 Here is my code: 这是我的代码:

Entity: 实体:

    /**
     * @ORM\Column(type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $id;

In my controller: 在我的控制器中:

    /**
     * @RestRoute\Post("")
     * @RestRoute\Post("/")
     */
    public function createAction($version, Request $request)
    {
        $res = $this->getManager()->createFromData(
            (array) $request->request->all()
        );

        if ($res) {
            return new JsonResponse($request->request->all());
        }
    }

And in my service/manager 在我的服务/经理中

    /**
     * Adds a new record to the entity table
     *
     * @param   CwPackageType   $data
     * @return  CwPackageType   the newly created entity
     * @throws  Exception       throws database/doctrine exception
     */
    public function createFromData($data)
    {
        $this->getEntityManager()->getConnection()->beginTransaction();

        try {
            $rec = (new CwPackageType())->createFromData($data);

            $this->getEntityManager()->persist($rec);
            $this->getEntityManager()->flush($rec);
            $this->getEntityManager()->getConnection()->commit($rec);

            return $rec;
        } catch (Exception $e) {
            $this->getEntityManager()->getConnection()->rollback();

            throw $e;
        }
    }

When the record is added to the table, the ID field is left as null/blank. 当记录添加到表中时,ID字段保留为空/空白。 If I set the STRICT_TRANS_TABLES mode on MySQL I get an error when trying to add the record to the database: 如果我在MySQL上设置STRICT_TRANS_TABLES模式,则尝试将记录添加到数据库时收到错误消息:

General error: 1364 Field 'id' doesn't have a default value

Without that mode, the record is added, but the UUID is null/blank 如果没有该模式,则会添加记录,但是UUID为空/空白

Note also that if I use the following annotation, the auto generated ID is properly created and stored in the ID field in the table 还要注意,如果我使用以下注释,则会正确创建自动生成的ID,并将其存储在表的ID字段中

    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */

What am I doing wrong? 我究竟做错了什么?

I know this does not answer your question, but I think it might still be helpful. 我知道这不能回答您的问题,但我认为它可能仍然有帮助。 One of the advantages of UUID is, that they can be generated before inserting the entity. UUID的优点之一是,可以在插入实体之前生成它们。 That means you have an identifier before it's saved that you can refer to. 这意味着在保存之前,您有一个可以引用的标识符。 In order for this to work, you would use a library to generate a UUID and then perform this in the constructor. 为了使它起作用,您将使用一个库来生成UUID,然后在构造函数中执行此操作。 The most common one for this, as far as I can tell, is ramsey/uuid and in the context of Symfony and Doctrine ramsey/uuid-doctrine . 据我所知,最常见的一种是ramsey / uuid ,在Symfony和Doctrine的上下文中,是ramsey / uuid-doctrine With this your entity would probably look something like this: 有了这个,您的实体可能看起来像这样:

/**
 * @ORM\Entity()
 */
class Foo
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="uuid")
     */
    private $id;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }

    public function getId(): string
    {
        return $this->id->toString();
    }

    //...
}

If you don't like this, you will still be able to reuse your old logic, just switch to a custom generator, pointing to the one from the library: 如果您不喜欢这样,您仍然可以重用旧的逻辑,只需切换到自定义生成器,指向库中的一个即可:

 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM