Doctrine referenced objects inserting null instead of value #5533

Closed
opened 2026-01-22 15:10:16 +01:00 by admin · 5 comments
Owner

Originally created by @smilesrg on GitHub (May 10, 2017).

Originally assigned to: @Ocramius on GitHub.

Here is a complex case. I have an RabbitMQ consumer, which processes messages using Doctrine2. Here is the steps:

  1. Consumer processes messages and saving to database. It saves several entities, using EntityManager:getReference() to set up referenced ids and not to execute redundant queries. For example, set user_id but not to execute "SELECT..." to get User entity from database. EntityManager::clear() is executed after this procedure.
  2. Record dissapears from database (I am deleting it manually but other consumer could delete it too).
  3. When Consumer tries to save new record to database, exception appears:
    Error "An exception occurred while executing 'INSERT INTO address_activity (last_send_datetime, last_delivery_datetime, last_read_datetime, last_click_datetime, address_id, user_id) VALUES (?, ?, ?, ?, ?, ?)' with params [null, null, "2017-05-10 15:52:50", "2017-05-10 15:52:50", null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'address_id' cannot be null"

address_id and user_id are the columns that I am referencing:

$addressReference = $this->entityManager->getReference(Address::class, $object->contactId);
$userReference = $this->entityManager->getReference(User::class, $object->userId);
$addressActivity->setAddress($addressReference);
$addressActivity->setUser($userReference);

It is okay that entity is being inserted, but it is not okay that address_id and user_id values are null, while var_dump() showing that they are actually not null:

var_dump($addressActivity->getAddress()->getId(), $addressActivity->getUser()->getId());
Originally created by @smilesrg on GitHub (May 10, 2017). Originally assigned to: @Ocramius on GitHub. Here is a complex case. I have an RabbitMQ consumer, which processes messages using Doctrine2. Here is the steps: 1. Consumer processes messages and saving to database. It saves several entities, using EntityManager:getReference() to set up referenced ids and not to execute redundant queries. For example, set user_id but not to execute "SELECT..." to get User entity from database. EntityManager::clear() is executed after this procedure. 2. Record dissapears from database (I am deleting it manually but other consumer could delete it too). 3. When Consumer tries to save new record to database, exception appears: ```Error "An exception occurred while executing 'INSERT INTO address_activity (last_send_datetime, last_delivery_datetime, last_read_datetime, last_click_datetime, address_id, user_id) VALUES (?, ?, ?, ?, ?, ?)' with params [null, null, "2017-05-10 15:52:50", "2017-05-10 15:52:50", null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'address_id' cannot be null"``` address_id and user_id are the columns that I am referencing: ```php $addressReference = $this->entityManager->getReference(Address::class, $object->contactId); $userReference = $this->entityManager->getReference(User::class, $object->userId); $addressActivity->setAddress($addressReference); $addressActivity->setUser($userReference); ``` It is okay that entity is being inserted, but it is not okay that address_id and user_id values are null, while var_dump() showing that they are actually not null: ```php var_dump($addressActivity->getAddress()->getId(), $addressActivity->getUser()->getId()); ```
admin added the BugInvalid labels 2026-01-22 15:10:16 +01:00
admin closed this issue 2026-01-22 15:10:17 +01:00
Author
Owner

@Ocramius commented on GitHub (May 10, 2017):

Can you check whether those values are also not null when reading via
reflection?

On 10 May 2017 6:38 p.m., "Sergii Smirnov" notifications@github.com wrote:

Here is a complex case. I have an RabbitMQ consumer, which processes
messages using Doctrine2. Here is the steps:

  1. Consumer processes messages and saving to database. It saves
    several entities, using EntityManager:getReference() to set up referenced
    ids and not to execute redundant queries. For example, set user_id but not
    to execute "SELECT..." to get User entity from database.
    EntityManager::clear() is executed after this procedure.
  2. Record dissapears from database (I am deleting it manually but
    other consumer could delete it too).
  3. When Consumer tries to save new record to database, exception
    appears:
    Error "An exception occurred while executing 'INSERT INTO
    address_activity (last_send_datetime, last_delivery_datetime,
    last_read_datetime, last_click_datetime, address_id, user_id) VALUES (?, ?,
    ?, ?, ?, ?)' with params [null, null, "2017-05-10 15:52:50", "2017-05-10
    15:52:50", null, null]: SQLSTATE[23000]: Integrity constraint violation:
    1048 Column 'address_id' cannot be null"

address_id and user_id are the columns that I am referencing:

$addressReference = $this->entityManager->getReference(Address::class, $object->contactId);$userReference = $this->entityManager->getReference(User::class, $object->userId);$addressActivity->setAddress($addressReference);$addressActivity->setUser($userReference);

It is okay that entity is being inserted, but it is not okay that
address_id and user_id values are null, while var_dump() showing that they
are actually not null:

var_dump($addressActivity->getAddress()->getId(), $addressActivity->getUser()->getId());


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6437, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakHvk6HXkjmxijs4cxgQnYNCrTAeAks5r4ef1gaJpZM4NW7R-
.

@Ocramius commented on GitHub (May 10, 2017): Can you check whether those values are also not `null` when reading via reflection? On 10 May 2017 6:38 p.m., "Sergii Smirnov" <notifications@github.com> wrote: > Here is a complex case. I have an RabbitMQ consumer, which processes > messages using Doctrine2. Here is the steps: > > 1. Consumer processes messages and saving to database. It saves > several entities, using EntityManager:getReference() to set up referenced > ids and not to execute redundant queries. For example, set user_id but not > to execute "SELECT..." to get User entity from database. > EntityManager::clear() is executed after this procedure. > 2. Record dissapears from database (I am deleting it manually but > other consumer could delete it too). > 3. When Consumer tries to save new record to database, exception > appears: > Error "An exception occurred while executing 'INSERT INTO > address_activity (last_send_datetime, last_delivery_datetime, > last_read_datetime, last_click_datetime, address_id, user_id) VALUES (?, ?, > ?, ?, ?, ?)' with params [null, null, "2017-05-10 15:52:50", "2017-05-10 > 15:52:50", null, null]: SQLSTATE[23000]: Integrity constraint violation: > 1048 Column 'address_id' cannot be null" > > address_id and user_id are the columns that I am referencing: > > $addressReference = $this->entityManager->getReference(Address::class, $object->contactId);$userReference = $this->entityManager->getReference(User::class, $object->userId);$addressActivity->setAddress($addressReference);$addressActivity->setUser($userReference); > > It is okay that entity is being inserted, but it is not okay that > address_id and user_id values are null, while var_dump() showing that they > are actually not null: > > var_dump($addressActivity->getAddress()->getId(), $addressActivity->getUser()->getId()); > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6437>, or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakHvk6HXkjmxijs4cxgQnYNCrTAeAks5r4ef1gaJpZM4NW7R-> > . >
Author
Owner

@smilesrg commented on GitHub (Jun 30, 2017):

@Ocramius Not a doctrine bug, closing this issue.

@smilesrg commented on GitHub (Jun 30, 2017): @Ocramius Not a doctrine bug, closing this issue.
Author
Owner

@avlima commented on GitHub (May 2, 2018):

Hello @smilesrg , what did you to to fix this? I'm having same issue

@avlima commented on GitHub (May 2, 2018): Hello @smilesrg , what did you to to fix this? I'm having same issue
Author
Owner

@gabrielebarchiesi commented on GitHub (Mar 12, 2021):

I have experienced same issue, UnitOfWork::executeInserts($class) is called instead of nothing. Only for very particular issues, maybe when repository uses useQueryCache(true) and/or useResultCache(true). I changed some logic and now seems to be ok.

@gabrielebarchiesi commented on GitHub (Mar 12, 2021): I have experienced same issue, UnitOfWork::executeInserts($class) is called instead of nothing. Only for very particular issues, maybe when repository uses useQueryCache(true) and/or useResultCache(true). I changed some logic and now seems to be ok.
Author
Owner

@smilesrg commented on GitHub (Jul 19, 2021):

I guess the problem was that I used different entity managers

@smilesrg commented on GitHub (Jul 19, 2021): I guess the problem was that I used different entity managers
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5533