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

Closed
doctrinebot opened this Issue Jul 1, 2013 · 2 comments

2 participants

@doctrinebot

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" }
@doctrinebot

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

Issue was closed with resolution "Invalid"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment