Explicitly set Id with Doctrine when using “AUTO” strategy

前端 未结 7 710
时光说笑
时光说笑 2020-12-02 05:36

My entity uses this annotation for it\'s ID:

/**
 * @orm:Id
 * @orm:Column(type=\"integer\")
 * @orm:GeneratedValue(s         


        
7条回答
  •  心在旅途
    2020-12-02 05:58

    Inspired by Villermen work, I created the library tseho/doctrine-assigned-identity which allows you to manually assign IDs to a Doctrine entity, even when the entity uses the stategies AUTO, SEQUENCE, IDENTITY or UUID.

    You should never use it in production but it's really useful for functional tests.

    The library will detect automatically the entities with an assigned id and replace the generator only when needed. The library will fallback on the initial generator when an instance does not have an assigned id.

    The replacement of the generator occurs in a Doctrine EventListener, no need to add any additional code in your fixtures.

提交回复
热议问题