cycle / orm

PHP DataMapper, ORM
https://cycle-orm.dev
MIT License
1.24k stars 72 forks source link

🐛 Several entities broken insert order #380

Closed gam6itko closed 9 months ago

gam6itko commented 2 years ago

No duplicates 🥲.

What happened?

If you save multiple related entities in the same transaction, there may be saved in broken order.

$em = (new EntityManager($orm));
$em->persist($user);

$i = 1;
$em->persist(new User\Alias($user, "{$user->age}-$i")) and $i++;

$em->persist(new User\Email($user, "{$user->age}-$i")) and $i++;
$em->persist(new User\Email($user, "{$user->age}-$i")) and $i++;

$em->persist(new User\Phone($user, "{$user->age}-$i")) and $i++;
$em->persist(new User\Phone($user, "{$user->age}-$i")) and $i++;
$em->persist(new User\Phone($user, "{$user->age}-$i")) and $i++;

$em->run();

will be saved in db in broken order

{
  "alias": [
    {
      "value": "246-1"
    }
  ],
  "email": [
    {
      "value": "246-2"
    },
    {
      "value": "246-3"
    }
  ],
  "phone": [
    {
      "value": "246-6"
    },
    {
      "value": "246-4"
    },
    {
      "value": "246-5"
    }
  ]
}

Bug demo: https://github.com/gam6itko/app/commit/70024f6c50527b2e21f25e5b3cc33217f32dd8c7

Bug detected on: http://localhost:8080/issue1 http://localhost:8080/issue2 http://localhost:8080/issue3

Bug NOT detected on: http://localhost:8080/no_issue1 http://localhost:8080/no_issue2

Version

ORM 2.2.0 
PHP 8.1.2

cycle/annotated                   v3.2.0          Cycle ORM Annotated Entities generator
cycle/database                    2.2.2           DBAL, schema introspection, migration and pagination
cycle/migrations                  v4.0.0          Database migrations, migration scaffolding
cycle/orm                         v2.2.0          PHP DataMapper ORM and Data Modelling Engine
cycle/schema-builder              v2.1.0          Cycle ORM Schema Builder
cycle/schema-migrations-generator 3.x-dev df0e391 Cycle ORM Migration generation
cycle/schema-renderer             1.1.0           Utils for Cycle ORM Schema rendering
gam6itko commented 10 months ago

The big trouble if I try to delete and insert entities with unique index.

See here: https://github.com/cycle/orm/actions/runs/7699320457/job/20980653390?pr=381#step:11:38