Search code examples
phpdoctrinesymfony4entitymanager

Doctrine only inserts one record after foreach loop


I have a simple array with some values that need to be inserted into the DB. Out of all the values only the last one is actually inserted.

When I put the flush(); inside the loop, records do get inserted.

When I dump the entity manager before the flush (outside the foreach) I do see references to all the values (entities).

Though, only the last record is inserted. It does get id #3, so it seems the other ones are lost somewhere.

    $values = [
        "val1", "val2", "val3"
    ];

    foreach ($values as $value) {
      $i = new MyEntityClass();
      $i->setVerified(false);
      $i->setName($value);
      $this->em->persist($i);
    }
    $this->em->flush();

Update: I hooked up a eventlistener to pre and post flush.

  public function preFlush(PreFlushEventArgs $args) {
    $em = $args->getEntityManager();

    foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
      dump($entity->getName());
    }
  }

  public function postFlush(PostFlushEventArgs $args) {
    dd($args->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions());
  }

In the preFlush all values are clearly printed and the postFlush dump is empty.

Update 2: I'm using a uuid_binary_ordered_time as follows

  /**
   * @ORM\Id
   * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
   * @GeneratedValue(strategy="CUSTOM")
   * @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
   * @Groups({"uuid"})
   */
  protected $id;

Using 10.4.8-MariaDB and php v7.3.10

Update 3: I'm still trying to figure this out and and now have a different scenario, but am still running into the problem that multiple actions are not executed. Might this be DB configuration related instead of Doctrine?

Case: Gather data from table A and table B, insert data into Table C, delete record from table A.

By following the suggestion from @Jakumi (or looking into the logs) I can see the following queries being executed (spoiler, the row in table A is deleted, but there's no new row in table C):

"queries": {
  "1": {
    "sql": "SELECT t0.email AS email_1, t0.status AS status_2, t0.payment_provider_id AS payment_provider_id_3, t0.payment_method AS payment_method_4, t0.quantity AS quantity_5, t0.price_total AS price_total_6, t0.created_on AS created_on_7, t0.updated_on AS updated_on_8, t0.paid_on AS paid_on_9, t0.id AS id_10, t0.s_uuid AS s_uuid_11, t0.product_id AS product_id_12 FROM payment_pending t0 WHERE t0.payment_provider_id = ? LIMIT 1",
    "params": [
      "tr_S432rV6fhM"
    ],
    "types": [
      "string"
    ],
    "executionMS": 0.0005559921264648438
  },
  "2": {
    "sql": "SELECT t0.username AS username_1, t0.roles AS roles_2, t0.password AS password_3, t0.email AS email_4, t0.email_verified AS email_verified_5, t0.created_on AS created_on_6, t0.registration_method AS registration_method_7, t0.has_premium AS has_premium_8, t0.premium_until AS premium_until_9, t0.verified_mobile AS verified_mobile_10, t0.active AS active_11, t0.facebook_id AS facebook_id_12, t0.google_id AS google_id_13, t0.id AS id_14, t0.s_uuid AS s_uuid_15, t16.is_a AS is_a_17, t16.wants_a AS wants_a_18, t16.firstname AS firstname_19, t16.lastname AS lastname_20, t16.screen_name AS screen_name_21, t16.function_title AS function_title_22, t16.mobile_number AS mobile_number_23, t16.birthday AS birthday_24, t16.age AS age_25, t16.zipcode AS zipcode_26, t16.city AS city_27, t16.id AS id_28, t16.s_uuid AS s_uuid_29, t16.user_id AS user_id_30 FROM user t0 LEFT JOIN user_extra t16 ON t16.user_id = t0.id WHERE t0.email = ? LIMIT 1",
    "params": [
      "some@email.com"
    ],
    "types": [
      "string"
    ],
    "executionMS": 0.0007460117340087891
  },
  "3": {
    "sql": "\"START TRANSACTION\"",
    "params": null,
    "types": null,
    "executionMS": 0.00010800361633300781
  },
  "4": {
    "sql": "INSERT INTO user_payments (status, payment_provider_id, payment_method, quantity, price_total, created_on, updated_on, paid_on, id, s_uuid, user_id, product_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    "params": {
      "1": "open",
      "2": "tr_S432rV6fhM",
      "3": "paypal",
      "4": 1,
      "5": "17.95",
      "6": "2019-10-24T07:27:22+00:00",
      "7": null,
      "8": null,
      "9": "9dd4af76-f630-11e9-90f2-024216133c1a",
      "10": "9dd4af76-f630-11e9-90f2-024216133c1a",
      "11": "c2abb28c-f62f-11e9-b71f-024216133c1a",
      "12": "bce559e8-f5bc-11e9-85f8-024216133c1a"
    },
    "types": {
      "1": "string",
      "2": "string",
      "3": "string",
      "4": "integer",
      "5": "decimal",
      "6": "datetime",
      "7": "datetime",
      "8": "datetime",
      "9": "uuid_binary_ordered_time",
      "10": "uuid",
      "11": "uuid_binary_ordered_time",
      "12": "uuid_binary_ordered_time"
    },
    "executionMS": 0.0003437995910644531
  },
  "5": {
    "sql": "DELETE FROM payment_pending WHERE id = ?",
    "params": [
      "b836c012-f62f-11e9-80b0-024216133c1a"
    ],
    "types": [
      "uuid_binary_ordered_time"
    ],
    "executionMS": 0.0003409385681152344
  },
  "6": {
    "sql": "\"COMMIT\"",
    "params": null,
    "types": null,
    "executionMS": 0.09281802177429199
  }
},
"enabled": true,
"start": 1571902427.83485,
"currentQuery": 6

Solution

  • I'm still not exactly sure what caused this, but I did manage to 'solve' the issue. I did so by completely removing MariaDB, its components and the database itself from my dev environment.

    After re-installing MariaDB, running mysql_secure_installation, re-creating the database and running migrations, all works as expected.

    $this->em->flush(); outside the foreach now inserts all records and same goes for my second problem case.