DDC-2537: One to one relation requires a join to get the details for the proxy object #3180

Closed
opened 2026-01-22 14:14:42 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 1, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user legolas:

I have the entities Cart(owning side) and Customer(inverse side) sharing bidirectional one to one relation. Now when I get an entity of cart, the proxy object for the customer is automatically linked to it. But when I ask that proxy object of customer for its name, it performs a join query with the cart and customer table to fetch it instead of just querying the customer table with the corresponding customer id present with the cart entity. The one to many relation works fine as explained in documentation. But this kind of strange thing is happening with one to one relation.
The following code explains the detail.

class Cart
{
    /****
     * @ORM\OneToOne(targetEntity="Customer", inversedBy="cart")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id",nullable=false)
     ****/  
    private $customer;
}
class Customer
{
    /****
     * @ORM\OneToOne(targetEntity="Cart", mappedBy="customer")
     ****/
    private $cart;
}

On running this code:
$cart = $em->find("\ZC\Entity\Cart", 12);
$cust = $cart->getCustomer();
print($cust->getName());

//The queries run are :
SELECT t0.id AS id1, t0.customer*id AS customer_id2 FROM cart t0 WHERE t0.id = ? array(1) { [0]=> int(12) } array(1) { [0]=> string(7) "integer" } SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.customer_id AS customer_id5 FROM customer t0 LEFT JOIN cart t3 ON t3.customer*id = t0.id WHERE t0.id = ? array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(7) "integer" }
Originally created by @doctrinebot on GitHub (Jul 1, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user legolas: I have the entities Cart(owning side) and Customer(inverse side) sharing bidirectional one to one relation. Now when I get an entity of cart, the proxy object for the customer is automatically linked to it. But when I ask that proxy object of customer for its name, it performs a join query with the cart and customer table to fetch it instead of just querying the customer table with the corresponding customer id present with the cart entity. The one to many relation works fine as explained in documentation. But this kind of strange thing is happening with one to one relation. The following code explains the detail. ``` class Cart { /**** * @ORM\OneToOne(targetEntity="Customer", inversedBy="cart") * @ORM\JoinColumn(name="customer_id", referencedColumnName="id",nullable=false) ****/ private $customer; } class Customer { /**** * @ORM\OneToOne(targetEntity="Cart", mappedBy="customer") ****/ private $cart; } On running this code: $cart = $em->find("\ZC\Entity\Cart", 12); $cust = $cart->getCustomer(); print($cust->getName()); //The queries run are : SELECT t0.id AS id1, t0.customer*id AS customer_id2 FROM cart t0 WHERE t0.id = ? array(1) { [0]=> int(12) } array(1) { [0]=> string(7) "integer" } SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.customer_id AS customer_id5 FROM customer t0 LEFT JOIN cart t3 ON t3.customer*id = t0.id WHERE t0.id = ? array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(7) "integer" } ```
admin added the Bug label 2026-01-22 14:14:42 +01:00
admin closed this issue 2026-01-22 14:14:43 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 1, 2013):

Comment created by legolas:

Okk I am mistaken. I am replying to my own post as I figured out the issue.
In the bidirectional one to one relation, whenever the inverse side entity is loaded, it fetches(eager) the owning side entity automatically that requires the join.
So this is not a bug.

@doctrinebot commented on GitHub (Jul 1, 2013): Comment created by legolas: Okk I am mistaken. I am replying to my own post as I figured out the issue. In the bidirectional one to one relation, whenever the inverse side entity is loaded, it fetches(eager) the owning side entity automatically that requires the join. So this is not a bug.
Author
Owner

@doctrinebot commented on GitHub (Aug 10, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Aug 10, 2013): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3180