I have a OneToOne relationship between Image and ImageMetadata, where ImageMetadata is optional, but in the cases where the Image does not have a related meta-data record, the listing fails:
An exception has been thrown during the rendering of a
template ("Entity of type 'App\Entity\ImageMetadata' for IDs imageid(2043) was not found").
The twig call is simply
{{ image.meta ? 'hasMeta' }}
which becomes a call to $image->getMeta().
I'm expecting it to return null if the related entity doesn't exist.
Image.php defines metadata as optional:
class Image
{
/**
* @ORM\Column(type="integer", name="id")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\ImageMetadata", orphanRemoval=true, fetch="EAGER")
* @ORM\JoinColumn(name="id", referencedColumnName="imageid", nullable=true)
*/
private $meta;
public function getMeta(): ?ImageMetadata
{
return $this->meta;
}
ImageMetadata.php uses the same key ("imageid")
class ImageMetadata
{
/**
* @ORM\Column(type="integer", name="imageid")
* @ORM\Id
*/
private $imageid;
Unfortunately, I don't have the option of tweaking the database, so I'm trying to define the Doctrine configuration to reflect the existing database and properly populate entities to use within the Symfony app. The table definitions are:
CREATE TABLE Images
(id INTEGER PRIMARY KEY, ...);
CREATE TABLE ImageMetadata (imageid INTEGER PRIMARY KEY,...);
There are images without metadata, but I can't figure out how to simply have doctrine return null when I call $image->getMeta(). I've tried the suggestions I've found here and in other forums, setting EAGER, setting it to null initially, removing orphans, etc., but I must still be missing something.
Thanks!