8

I am finding a entity by its PK as follow:

$ent = $em->getRepository('AppBundle:Representative')->find($id)

What is the right way to check whether $ent is a real Representative object or not? What I mean with real is that $ent currently exists on DB and was returned since I am planning to use the same results for INSERT and UPDATE. In pseudo-code what is on my head is:

if (ent is Representative)
{
    // Update its values
} else {
    // Create a new Representative
}

I was thinking in use is_object() or even instanceof but I am not sure if they will do the job or if $ent will be an object even if Representative doesn't exist on DB. Any advice on this? How I can achieve that?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • I must miss something because the way I see it, either your object exists in DB and find($id) will find it, and it will be a Representative object, or it returns null if not found. I don't think there is any need to check the type of the object ... – Guillaume Fache Jun 29 '15 at 12:37
  • @GuillaumeFache So you're saying that right way should be simply something like `!$ent` or `$ent !== null` on the conditional? That? – ReynierPM Jun 29 '15 at 12:39
  • 1
    Doctrine's default queries should return Null if the object is not found in the database. If your issue is whether or not the object in the database is valid you should look in to validation https://symfony.com/doc/current/book/validation.html – Squeegy Jun 29 '15 at 12:39
  • @ReynierPM I think that's enough yes (like what is said in the answer beneath) – Guillaume Fache Jun 29 '15 at 12:42

2 Answers2

15

EntityRepository::find() method (which you use) returns an object, or null if the object couldn't be found in the database. All of the following conditions are valid:

if ($entity) {
}

if (null !== $entity) {
}

if ($entity instanceof Representative) {
}

Choose one that suits your coding standards the best, and use it consistently.

If you don't need to create a new object if it's not found, better throw an exception and handle it appropriately.

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • In fact I will need to create a new object, please if you can take a look to [this other post](http://stackoverflow.com/questions/31116297/how-to-right-insert-or-update-on-same-doctrine2-object) which has some relation with this one, and thx for your answer – ReynierPM Jun 29 '15 at 12:55
2

How about this:

$product = $this->getDoctrine()
        ->getRepository('AppBundle:Product')
        ->find($id);

    if (!$product) {
        throw $this->createNotFoundException(
            'No product found for id '.$id
        );

Source: click me

bambamboole
  • 577
  • 10
  • 30
  • I wouldn't say throwing an exception is good in this case. When asking to find/get something it's known that the something may not exist. IMO return e.g. EITHER a bool and the caller checks for bool, OR return the entity|null and caller checks for object|null. Which one depends on if you actually need the entity if exists or not, if not then return bool just check true/false. – James Jan 25 '22 at 12:35