Problem
I'm trying to create a OneToOne association in a Laravel app using Doctrine. When trying to access the association I'm getting this error.
Entity of type 'Status' for IDs clientId(1) was not found
Versions:
Doctrine: 2.7.5
Laravel: 7.30.4
Code:
Client Class
<?php
namespace App\Client;
use App\Person\Person;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Client
* @package App\Client
*
* @ORM\Entity(repositoryClass="ClientRepository")
* @ORM\Table(name="client")
*/
class Client extends Person
{
/**
* @var Status
*
* @ORM\OneToOne(targetEntity="Status", mappedBy="client")
* @ORM\JoinColumn(name="id", referencedColumnName="client_id", nullable=true)
*/
protected $status;
/**
* @return Status
*/
public function getStatus()
{
return $this->status;
}
}
Status Class:
<?php
namespace App\Client\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Status
* @package App\Client\Entity
*
* @ORM\Entity(readOnly=true)
* @ORM\Table(name="status_view")
*/
class Status
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(name="client_id", type="integer")
*/
protected $clientId;
/**
* @var \App\Client\Client
*
* @ORM\OneToOne(targetEntity="App\Client\Client", inversedBy="staus")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=true)
*/
protected $client;
/**
* @var string
*
* @ORM\Column(name="status", type="string")
*/
protected $status;
/**
* @return string
*/
public function getStatus()
{
return $this->status;
}
}
Calling Code
$client->getStatus()->getStatus()
What I've tried/Answers I've looked at
- Entity of type 'AppBundle\Entity\User' for IDs id(155) was not found - I'm not using a Doctrine filter, nor DQL.
- https://stackoverflow.com/a/49416542/9530790 - This works, with a few tweaks, by swallowing up the exception, but it feels more like a hack when the docs say
nullable
should work. - https://stackoverflow.com/a/21887344/9530790 - This states
nullable
should work but it doesn't. - https://stackoverflow.com/a/15744449/9530790 - Same question different ans. States that Doctrine doesn't support zero-to-one associations, but
nullable
I believe should be what solves that, but for my problem it's not working. Also there's no link to the docs stating where Zero to one is not supported. - I believe that adding
fetch="EAGER"
should fix the null issue as elsewhere in our app that works, but when I add that I get an different Doctrine errorspl_object_hash() expects parameter 1 to be object, null given
, which again has to do with the association not existing. - "Well why aren't you experiencing the above error with your other associations". Great question! After a deep underwater excursion into the Doctrine code, I believe the reason is because those associations are nested and for some reason (I'm not sure why), when nested, the
spl_object_hash
function, in classUnitOfWork
is not called.
Additional Notes:
This is what the object looks like when calling
$client->getStatus()
, before it errors on the next->getStatus()
call.DoctrineProxies\__CG__\App\Client\Entity\Status {#2011 +__isInitialized__: false #clientId: 4 #client: null #status: null …2 }
You can see it's a
Client
Proxy object that's created not a 'true' object, this is why it errors (with Entity of type 'Status' for IDs clientId(1) was not found) when not usingfetch="EAGER"
, since eager loads a true object. See hereThis code below in the Proxy object is the what causes the above error. Which is why I can't do a
try catch
in the parent ('true'Client
class), since it errors before calling the parent./** * {@inheritDoc} */ public function getStatus() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getStatus', []); return parent::getStatus(); }
Question:
Why is nullable=true
not working as expected, and what should/can I do to make it work?