I have a PHP application with Doctrine 2 ORM. I call getter on an entity object. It works, but PHP shows fatal error saying I am calling a method on null, but is_null
function called with the same object as parameter (one line above the error) returns false. The weirdest part is that the method call returns an object and its field is printed without issues (see output).
I tried fetching other entities at the same place calling similar methods and doing nearly the same stuff without error. There is a problem just with PageUrl entities.
I think that my mappings are ok and there are no problems with the code itself. I suspect Doctrine or PHP built-in webserver. Any ideas what causes the error and why doesn't it stop execution?
Error
PHP Fatal error: Uncaught Error: Call to a member function getPage() on null in .../Core/Routing/UrlResolver.php:39
Stack trace:
#0 .../Core/Routing/Router.php(92): Cms\Core\Routing\UrlResolver->getPageUrlForCurrentUrl()
#1 .../Core/Routing/Router.php(53): Cms\Core\Routing\Router->getPageUrl()
#2 .../Cms.php(21): Cms\Core\Routing\Router->process()
#3 ...index.php(12): Cms\Cms->run()
#4 ...built-in-server-routing.php(6): include('...')
#5 {main}
thrown in .../Core/Routing/UrlResolver.php on line 39
Where the error happens
/**
* @return PageUrl|null
*/
public function getPageUrlForCurrentUrl(): ?PageUrl
{
$relativeUrl = $this->currentUrl->getRelativeUrl();
$url = $this->entityManager
->getRepository(PageUrl::class)
->findOneBy(['url' => $relativeUrl]);
var_dump(is_null($url));
var_dump($url->getPage()->getName()); // LINE 39 - FATAL ERROR
return $url;
}
Output
bool(false)
string(8) "Homepage"
PageUrl entity
/**
* @ORM\Table(name="page_url")
* @ORM\Entity
*/
class PageUrl
{
/**
* @var string
* @ORM\Id
* @ORM\Column(type="string", length=200, nullable=false)
*/
protected $url;
/**
* @var Page
* @ORM\ManyToOne(targetEntity="Page", inversedBy="pageUrls")
* @ORM\JoinColumn(name="page_id", referencedColumnName="id")
*/
protected $page;
/**
* @return Page
*/
public function getPage(): Page
{
return $this->page;
}
...
}
Page entity
/**
* @ORM\Entity
*/
class Page
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var Collection
* @ORM\OneToMany(targetEntity="PageUrl", mappedBy="page")
*/
protected $pageUrls;
...
}