DDC-825: NoEntityFoundError when using OneToOne-Relationship, when it is possible that the target is empty #1016

Open
opened 2026-01-22 12:59:00 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 3, 2010).

Jira issue originally created by user le_shatai:

When I try to access a property of the client, that the client is in a OneToOne-Relationship with and the target entity does not exists, the NoEntityFoundException is thrown, which of course is correct, as the entity indeed does not exists, but I can not make sure it exists.
So in fact I have a 1....? Relationship, where ? is 0 or 1. How is it possible to avoid that exception, an Doctrine returns an empty target object ?

=============== THE DAO ===============



<?php



namespace fp\dao;

use fp\EntityManagerFactory;



class ClientDao {





    public function findAll($limit=20, $offset=0, $partial = array() ){

        $emf = EntityManagerFactory::createFactory();

        $em = $emf->getEntityManager();



        if( count($partial) != 0 ){

            $partialFields = '.{'.implode(',', $partial).'}';

            $query = $em->createQuery('SELECT partial c'.$partialFields.' FROM fp\entity\Client c WHERE c.sichtbar=1')->setMaxResults($limit);

        }

        else

            $query = $em->createQuery('SELECT c FROM fp\entity\Client c WHERE c.sichtbar=1')->setMaxResults($limit);



        return $query->getResult();

    }



    public function findById( $id ){

        $emf = EntityManagerFactory::createFactory();

        $em = $emf->getEntityManager();

        #$query = $em->createQuery("SELECT c FROM fp\entity\Client c WHERE c.id= '".$id."'");

        $query = $em->createQuery("SELECT c FROM fp\entity\Client c LEFT JOIN c.sozialversicherung sv  WHERE c.id= '".$id."'");

        return $query->getSingleResult();

    }



}

?>



=============== THE CLIENT ENTITY ===============



<?php



namespace fp\entity;

use fp\BaseEntity;



/****

 * @Entity

 * @Table(name="Adr")

 */

class Client extends BaseEntity {



    /****

     * @Id

     * @GeneratedValue(strategy="NONE")

     * @Column(name="AdrId", type="string")

     */

    private $id;



    /****

     * @Column(name="vorname", type="string")

     */

    private $vorname;



    /****

     * @Column(name="name", type="string")

     */

    private $nachname;



    [...] 

        LOADS OF PROPERTIES 

    [...]



    /****

     * @OneToOne(targetEntity="SocialInsurance")

     * @JoinColumn(name="AdrId", referencedColumnName="AdrId")

     */

    private $sozialversicherung;







    public function **construct(){

       $this->sozialversicherung = new SocialInsurance();

    }



    [...] 

        LOADS OF GETTERS/SETTERS 

    [...]



}

?>


=============== THE SOCIALINSURANCE ENTITY ===============


<?php



namespace fp\entity;

use fp\BaseEntity;



/****

 * @Entity

 * @Table(name="sozialversicherung")

 */

class SocialInsurance extends BaseEntity{



    /****

     * @Id

     * @GeneratedValue(strategy="NONE")

     * @Column(name="AdrId", type="string")

     */

    private $id;



    /****

     * @Column(name="Sozialversicherung_ID", type="string")

     */

    private $nummer;



    public function getId() {

        return $this->id;

    }



    public function setId($id) {

        $this->id = $id;

    }



    public function getNummer() {

        return $this->nummer;

    }



    public function setNummer($nummer) {

        $this->nummer = $nummer;

    }





}

?>







=============== THE OUTPUT ===============



<h1> Mandanteninfo </h1>



<div>

    <span><a href="<?php echo url_for('client/index')?>">Mandantenliste</a></span>

</div>

<h3>Allgemein</h3>

<table>

    <tr>

        <th>Vorname</th>

        <th>Nachname</th>

    </tr>

    <tr>

        <td><?php echo $client->vorname; ?></td>

        <td><?php echo $client->nachname; ?></td>

    </tr>

</table>



<h3>Sozialvers.</h3>

<table>

    <tr>

        <th>Sozialversicherung</th>

        <td><?php echo $client->sozialversicherung->nummer; ?></td>

    </tr>

</table>




===================================

It crashes when I try to access the sozialversicherungs object, which does not exists, as there is no corresponding
entry is the table.

Originally created by @doctrinebot on GitHub (Oct 3, 2010). Jira issue originally created by user le_shatai: When I try to access a property of the client, that the client is in a OneToOne-Relationship with and the target entity does not exists, the NoEntityFoundException is thrown, which of course is correct, as the entity indeed does not exists, but I can not make sure it exists. So in fact I have a 1....? Relationship, where ? is 0 or 1. How is it possible to avoid that exception, an Doctrine returns an empty target object ? ``` =============== THE DAO =============== <?php namespace fp\dao; use fp\EntityManagerFactory; class ClientDao { public function findAll($limit=20, $offset=0, $partial = array() ){ $emf = EntityManagerFactory::createFactory(); $em = $emf->getEntityManager(); if( count($partial) != 0 ){ $partialFields = '.{'.implode(',', $partial).'}'; $query = $em->createQuery('SELECT partial c'.$partialFields.' FROM fp\entity\Client c WHERE c.sichtbar=1')->setMaxResults($limit); } else $query = $em->createQuery('SELECT c FROM fp\entity\Client c WHERE c.sichtbar=1')->setMaxResults($limit); return $query->getResult(); } public function findById( $id ){ $emf = EntityManagerFactory::createFactory(); $em = $emf->getEntityManager(); #$query = $em->createQuery("SELECT c FROM fp\entity\Client c WHERE c.id= '".$id."'"); $query = $em->createQuery("SELECT c FROM fp\entity\Client c LEFT JOIN c.sozialversicherung sv WHERE c.id= '".$id."'"); return $query->getSingleResult(); } } ?> =============== THE CLIENT ENTITY =============== <?php namespace fp\entity; use fp\BaseEntity; /**** * @Entity * @Table(name="Adr") */ class Client extends BaseEntity { /**** * @Id * @GeneratedValue(strategy="NONE") * @Column(name="AdrId", type="string") */ private $id; /**** * @Column(name="vorname", type="string") */ private $vorname; /**** * @Column(name="name", type="string") */ private $nachname; [...] LOADS OF PROPERTIES [...] /**** * @OneToOne(targetEntity="SocialInsurance") * @JoinColumn(name="AdrId", referencedColumnName="AdrId") */ private $sozialversicherung; public function **construct(){ $this->sozialversicherung = new SocialInsurance(); } [...] LOADS OF GETTERS/SETTERS [...] } ?> =============== THE SOCIALINSURANCE ENTITY =============== <?php namespace fp\entity; use fp\BaseEntity; /**** * @Entity * @Table(name="sozialversicherung") */ class SocialInsurance extends BaseEntity{ /**** * @Id * @GeneratedValue(strategy="NONE") * @Column(name="AdrId", type="string") */ private $id; /**** * @Column(name="Sozialversicherung_ID", type="string") */ private $nummer; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getNummer() { return $this->nummer; } public function setNummer($nummer) { $this->nummer = $nummer; } } ?> =============== THE OUTPUT =============== <h1> Mandanteninfo </h1> <div> <span><a href="<?php echo url_for('client/index')?>">Mandantenliste</a></span> </div> <h3>Allgemein</h3> <table> <tr> <th>Vorname</th> <th>Nachname</th> </tr> <tr> <td><?php echo $client->vorname; ?></td> <td><?php echo $client->nachname; ?></td> </tr> </table> <h3>Sozialvers.</h3> <table> <tr> <th>Sozialversicherung</th> <td><?php echo $client->sozialversicherung->nummer; ?></td> </tr> </table> =================================== ``` It crashes when I try to access the sozialversicherungs object, which does not exists, as there is no corresponding entry is the table.
admin added the Bug label 2026-01-22 12:59:00 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1016