PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] Call to a member function on null in Symfony

 January 21, 2022     doctrine, doctrine-orm, php, symfony, symfony4     No comments   

Issue

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;

Solution

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

'seats' => is_null($seats) ? 0 : $seats->getSeats()


Answered By - Chris Haas
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing