Eric Marigoh
Eric Marigoh

Reputation: 173

Symfony: Resolve Doctrine EntityNotFoundException

I have a pre-existing Oracle database that I want to exploit.

To do this, I created 2 classes that I map with many-to-one and one-to-many. But there are elements of which the first class have no correspondence in the second class, however id exists. It does not return any error.

I would like that it sends me rather a null. How to do that ?

class Fact
{

/**
     * 
     * @ORM\OneToMany(targetEntity="present\UserBundle\Entity\March",mappedBy="fact", cascade={"persist","remove"})     
     */
    private $march;
}

class March
{
   ...

     /*
     * @var \present\PublishBundle\Entity\image
        * @ORM\ManyToOne(targetEntity="present\UserBundle\Entity\fact",inversedBy="march", cascade={"persist"}) 
      * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id",referencedColumnName="id")
     * })
     * })  
     */
     private $facture;
     }

the error

Entity of type 'present\UserBundle\Entity\Client' for IDs id(0) was not found

Thanx you

Upvotes: 0

Views: 1768

Answers (2)

Evaldo Junior
Evaldo Junior

Reputation: 407

I know it's quite late for this discussion, but I found a possible solution. It's possible to use PHP's exceptions to handle such cases.

In my case I have a customer table that relates to zero or one address. So, I can have a happy path like this:

$streetName = $customer->getAddress()->getStreetName();
echo $streetName; // Baker Street

Or it may throw an exception, so a way to solve it is something like this:

use Doctrine\ORM\EntityNotFoundException;

try {
    $streetName = $customer->getAddress()->getStreetName();
} catch (EntityNotFoundException $e) {
    $streetName = 'No address provided!';
}

Upvotes: 1

Antoine Galluet
Antoine Galluet

Reputation: 320

I haven't tested your code but I think your error has to do your JoinColumn declaration :

@ORM\JoinColumn(name="id",referencedColumnName="id")

In the documentation here it says that the name attribute will define the name of the column for your relation. But here "id" is also referencing to your primary key I suppose.

To solve it, try to change the name="id" by name="march_id", or remove the JoinColumn.

Edit : I have read a bit to quickly the error, haven't seen it's referencing to Client entity, can you show the code related to this relation too ?

Upvotes: 1

Related Questions