Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2537

One to one relation requires a join to get the details for the proxy object

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.3.3
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Windows 8

      Description

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

        Activity

        Hide
        Koustubh Sinhal added a comment - - edited

        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.

        Show
        Koustubh Sinhal added a comment - - edited 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.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Koustubh Sinhal
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: