Doctrine optional OneToOne entity deletion

Viewed 21

I have two entities, a Flight and a Booking.

They are configured in a bi-directional one-to-one relationship, with the Flight underlying table holding the foreign key to the Booking underlying table.

The idea is that a Flight instance must be linked to a Booking, but a Booking can exist without a Flight.

/**
 * @ORM\Entity
 */
class Flight{

    /**
     * @ORM\OneToOne(targetEntity="Booking", inversedBy="flight")
     * @ORM\JoinColumn(name="bookingID", referencedColumnName="ID", nullable=false)
     */
    private Booking $booking;

    /// Rest of code removed for readability

} 
/**
 * @ORM\Entity
 */
class Booking{

    /**
     * @ORM\OneToOne(targetEntity="Flight", mappedBy="booking")
     */
    private ?Flight $flight;
    
    /// Rest of code removed for readability
}

I do not want to use cascading operations since a Flight can be removed without the Booking being removed (this is an extension of the idea stated above).

An issue arises when I try to delete both the Flight and the Booking.

Picture the following code:

// $flight holds a reference to $booking, as per the code above
$em->remove($flight);
$em->remove($booking);
$em->flush();

It would result in the following queries (logged by Doctrine)

START TRANSACTION
DELETE FROM flights WHERE ID = ?
INSERT INTO flights (redacted) VALUES (redacted)
DELETE FROM bookings WHERE ID = ?
ROLLBACK

Which I simply cannot understand.

If I remove the Booking directly through the underlying PDO Handle, it works perfectly, which means that is has to be a mapping issue, but I cannot see where I did something wrong. It's been driving me crazy for hours.

0 Answers
Related