-2

In my Symfony project I wrote a query with Doctrine to return one specific result which I will display in my custom array.

I constantly get an error:

Call to a member function getSeats() on null

I tried a bunch of things..

 return $this->createQueryBuilder("e")
        ->where('e.userId =:userId')
        ->setParameter('userId', $user)
        ->getQuery()
        ->getOneOrNullResult();

And in my array..

$reports = [
            'id' => $report['id'],
             ....
            'seats' => is_null($seats->getSeats()) ? 0 : $seats->getSeats()
        ];

I tought the best way to do it is by ternary operator but no luck/

My entity object:

/**
 * @ORM\Column(type="integer", options={"default":0})
 */
protected $seats;
  • 2
    You are binding the wrong parameter, the parameter is called `userId`, not `user`, that's why it's always returning null. – msg Nov 09 '20 at 22:53

1 Answers1

2

The is_null is correct, however you need to check whether the object itself is null:

'seats' => is_null($seats) ? 0 : $seats->getSeats()
Chris Haas
  • 53,986
  • 12
  • 141
  • 274