DDC-1867: Inserting objects in relation x to one, schedules extra update error #2354

Closed
opened 2026-01-22 13:49:17 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 11, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user moatux:

At the time of persistence (flush operation) of objects with x-to-one relationship, join IDs are positioned correctly but extra updates are planned, which will generate update in database by changing the identifiers that were previously correctly positioned.

scheduled update is performed in the basicEntityPersister then these extra updates are played in the unitOfWork.

To take a simple example, if you have two objects Hotel, Room in one to one relationship (for simplicity)

we then have the following code:

/ ****
 * @ Entity (repositoryClass = "Hotel")
 *
 * @ Table (name = "hotel")
 * /
Hotel class
{
    / ****
     * @ Id
     * @ Column (type = "integer", name = "id", nullable = false)
     * @ GeneratedValue (strategy = "AUTO")
     * /
    public $ id;

    / ****
     * @ Column (type = "string")
     * @ Var string
     * /
    public $ address;

    / ****
     * @ OneToOne (targetEntity = "Room",
     * MappedBy = "hotel",
     * Cascade = {"all"})
     * @ JoinColumn (name = "id", referencedColumnName = "hotel_id")
     * /
    public $ room;


    public function function setRoom(Room $room)
    {
          $this->room = $room;
          return $this;
    }
}

/ ****
 * @ Entity (repositoryClass = "Room")
 *
 * @ Table (name = "Room")
 * /
Class Room
{
    / ****
     * @ Id
     * @ Column (type = "integer", name = "id", nullable = false)
     * @ GeneratedValue (strategy = "AUTO")
     * /
    public $ id;

    / ****
     * @ Column (type = "integer", name = "property_id", nullable = false)
     * /
    public $ hotel_id ;

    / ****
     * @ OneToOne (targetEntity = "Hotel",
     * InversedBy = "room",
     * Cascade = {"all"})
     * @ JoinColumn (name = "hotel_id"
     * ReferencedColumnName = "id")
     * /
    public $ hotel;


}

It then creates:

$ Room = new Room;

$ Hotel = new Hotel;
$ Hotel-> address = 'test';
$ Hotel-> setRoom ($ room);

in the SQL log we have:

INSERT INTO Hotel (address, ....) VALUES ('test', ...)
INSERT INTO Room (Hotel_id, ...., id) VALUES (430, ....., NULL)

Persistence has positioned correctly Room.hotel_id = Hotel.id

But an extra update scheduled created this request

UPDATE Hotel SET id = NULL WHERE id = 430

To avoid this behavior, i had to comment these lines in the unitOfWork file : (around line 316)

            // Extra updates that were requested by persisters.
            if ($this->extraUpdates) {
//                $this->executeExtraUpdates();
            }

and all works well so.

Originally created by @doctrinebot on GitHub (Jun 11, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user moatux: At the time of persistence (flush operation) of objects with x-to-one relationship, join IDs are positioned correctly but extra updates are planned, which will generate update in database by changing the identifiers that were previously correctly positioned. scheduled update is performed in the basicEntityPersister then these extra updates are played in the unitOfWork. To take a simple example, if you have two objects Hotel, Room in one to one relationship (for simplicity) we then have the following code: ``` / **** * @ Entity (repositoryClass = "Hotel") * * @ Table (name = "hotel") * / Hotel class { / **** * @ Id * @ Column (type = "integer", name = "id", nullable = false) * @ GeneratedValue (strategy = "AUTO") * / public $ id; / **** * @ Column (type = "string") * @ Var string * / public $ address; / **** * @ OneToOne (targetEntity = "Room", * MappedBy = "hotel", * Cascade = {"all"}) * @ JoinColumn (name = "id", referencedColumnName = "hotel_id") * / public $ room; public function function setRoom(Room $room) { $this->room = $room; return $this; } } / **** * @ Entity (repositoryClass = "Room") * * @ Table (name = "Room") * / Class Room { / **** * @ Id * @ Column (type = "integer", name = "id", nullable = false) * @ GeneratedValue (strategy = "AUTO") * / public $ id; / **** * @ Column (type = "integer", name = "property_id", nullable = false) * / public $ hotel_id ; / **** * @ OneToOne (targetEntity = "Hotel", * InversedBy = "room", * Cascade = {"all"}) * @ JoinColumn (name = "hotel_id" * ReferencedColumnName = "id") * / public $ hotel; } ``` It then creates: ``` $ Room = new Room; $ Hotel = new Hotel; $ Hotel-> address = 'test'; $ Hotel-> setRoom ($ room); ``` in the SQL log we have: ``` INSERT INTO Hotel (address, ....) VALUES ('test', ...) INSERT INTO Room (Hotel_id, ...., id) VALUES (430, ....., NULL) ``` Persistence has positioned correctly Room.hotel_id = Hotel.id But an extra update scheduled created this request ``` UPDATE Hotel SET id = NULL WHERE id = 430 ``` To avoid this behavior, i had to comment these lines in the unitOfWork file : (around line 316) ``` // Extra updates that were requested by persisters. if ($this->extraUpdates) { // $this->executeExtraUpdates(); } ``` and all works well so.
admin added the Bug label 2026-01-22 13:49:17 +01:00
admin closed this issue 2026-01-22 13:49:17 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2012):

Comment created by moatux:

see DDC-171 key bug

@doctrinebot commented on GitHub (Jun 11, 2012): Comment created by moatux: see [DDC-171](http://www.doctrine-project.org/jira/browse/DDC-171) key bug
Author
Owner

@doctrinebot commented on GitHub (Jul 4, 2012):

Comment created by @beberlei:

fixed styles.

removing a feature is not a fix for something :)

@doctrinebot commented on GitHub (Jul 4, 2012): Comment created by @beberlei: fixed styles. removing a feature is not a fix for something :)
Author
Owner

@doctrinebot commented on GitHub (Jul 4, 2012):

Comment created by @beberlei:

Your mapping is wrong, you are not supposed to map both hotel_id and hotel. Just use $hotel and $hotel->getId() with 2.2 its optimized not to do extra lazy load queries.

@doctrinebot commented on GitHub (Jul 4, 2012): Comment created by @beberlei: Your mapping is wrong, you are not supposed to map both hotel_id and hotel. Just use $hotel and $hotel->getId() with 2.2 its optimized not to do extra lazy load queries.
Author
Owner

@doctrinebot commented on GitHub (Jul 4, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jul 4, 2012): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jul 5, 2012):

Comment created by moatux:

Removing this feature, I knew it was not the best solution but i didn't understand where was the problem.

I wanna say you thanks a lot for your anwser. You re wright, mapping was not correct.

Congratulations for this work, doctrines 2 is an awesome PHP project.

Thank you again.

@doctrinebot commented on GitHub (Jul 5, 2012): Comment created by moatux: Removing this feature, I knew it was not the best solution but i didn't understand where was the problem. I wanna say you thanks a lot for your anwser. You re wright, mapping was not correct. Congratulations for this work, doctrines 2 is an awesome PHP project. Thank you again.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2354