DDC-3165: one to zero or one with identity through foreign entity #3924

Open
opened 2026-01-22 14:31:35 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 13, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user andriesss:

<?php

/****
 * @Entity
 */
class User
{
    /**** 
     * @Id @Column(type="integer") 
     * @GeneratedValue 
     */
    private $id;

    /****
     * @ORM\OneToOne(targetEntity="Address")
     *
     * @var Address
     */
    private $address;

    public function getAddress()
    {
        return $this->address;
    }
}

/****
 * @Entity
 */
class Address
{
    /****
     * @Id @OneToOne(targetEntity="User") 
     */
    private $user;

    public function foo()
    {}
}


?>

The relation between user and address is one to zero or one. When a record exists on both sides, everything goes fine. When the right side (address) does not have a relevant record, calling $user->getAddress(); always returns an instance. Calling a method on that instance results in an exception:

Fatal error: Uncaught exception 'Doctrine\ORM\EntityNotFoundException' with message 'Entity of type 'Address' was not found.' in doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php on line 176

Expected behaviour:
$user->getAddress() should return NULL when the right side is empty.

NOTES:
Mind that the address is identified through a foreign entity (User)

Originally created by @doctrinebot on GitHub (Jun 13, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user andriesss: ``` <?php /**** * @Entity */ class User { /**** * @Id @Column(type="integer") * @GeneratedValue */ private $id; /**** * @ORM\OneToOne(targetEntity="Address") * * @var Address */ private $address; public function getAddress() { return $this->address; } } /**** * @Entity */ class Address { /**** * @Id @OneToOne(targetEntity="User") */ private $user; public function foo() {} } ?> ``` The relation between user and address is one to zero or one. When a record exists on both sides, everything goes fine. When the right side (address) does not have a relevant record, calling $user->getAddress(); always returns an instance. Calling a method on that instance results in an exception: Fatal error: Uncaught exception 'Doctrine\ORM\EntityNotFoundException' with message 'Entity of type 'Address' was not found.' in doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php on line 176 Expected behaviour: $user->getAddress() should return NULL when the right side is empty. NOTES: Mind that the address is identified through a foreign entity (User)
admin added the Bug label 2026-01-22 14:31:35 +01:00
Author
Owner

@felipyamorim commented on GitHub (Dec 22, 2016):

+1

@felipyamorim commented on GitHub (Dec 22, 2016): +1
Author
Owner

@elisavet1 commented on GitHub (Jun 8, 2018):

Did anyone found the solution for this?

@elisavet1 commented on GitHub (Jun 8, 2018): Did anyone found the solution for this?
Author
Owner

@ferran294 commented on GitHub (Oct 4, 2018):

Any solution to this?

@ferran294 commented on GitHub (Oct 4, 2018): Any solution to this?
Author
Owner

@ferran294 commented on GitHub (Oct 5, 2018):

Ok, I just have made this work. You should create your relation one to one with EAGER load so Doctrine searches in the database with a JOIN this relation. With the by default LAZY load of Doctrine the entities are not found when trying to be found with an exact WHERE clause when accessing to the object. So the structure you should define would be like this:

<?php

/****
 * @Entity
 */
class User
{
    /**** 
     * @Id @Column(type="integer") 
     * @GeneratedValue 
     */

    private $id;

  
     /**
     * @var Address
     * @ORM\OneToOne(targetEntity="Address",fetch="EAGER")
     * @ORM\JoinColumn(name="user_id",referencedColumnName="user_id",nullable=true)
     */
    private $address;

    public function getAddress(): ?Address
    {
        return $this->address;
    }
}

/****
 * @Entity
 */
class Address
{
    /****
     * @Id 
     */
    private $user;

    public function foo()
    {}
}


?>
@ferran294 commented on GitHub (Oct 5, 2018): Ok, I just have made this work. You should create your relation one to one with EAGER load so Doctrine searches in the database with a JOIN this relation. With the by default LAZY load of Doctrine the entities are not found when trying to be found with an exact WHERE clause when accessing to the object. So the structure you should define would be like this: ``` <?php /**** * @Entity */ class User { /**** * @Id @Column(type="integer") * @GeneratedValue */ private $id; /** * @var Address * @ORM\OneToOne(targetEntity="Address",fetch="EAGER") * @ORM\JoinColumn(name="user_id",referencedColumnName="user_id",nullable=true) */ private $address; public function getAddress(): ?Address { return $this->address; } } /**** * @Entity */ class Address { /**** * @Id */ private $user; public function foo() {} } ?> ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3924