douhao5280 2018-03-16 14:12
浏览 51

可选的相关学说实体(OneToOne关系)

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!

  • 写回答

1条回答 默认 最新

  • dotxxh0998 2018-03-16 14:37
    关注

    You are using the JoinColumn annotation on the wrong side of the relation. Try to do a bi-directional relation (as the owning side is defined by which entity has the relation id stored - which is the metadata in your case, so there the JoinColumn annotation belongs to).

    Something like this:

    class Image   
    {
       /**
        * @ORM\Column(type="integer", name="id")
        * @ORM\Id
        * @ORM\GeneratedValue
        */
        private $id;
    
        /**
         * @ORM\OneToOne(targetEntity="App\Entity\ImageMetadata", mappedBy="image", orphanRemoval=true, fetch="EAGER")
         */
    
        private $meta;
    
    class ImageMetadata
    {
    
       /**
        * @ORM\OneToOne(targetEntity="App\Entity\Image", inversedBy="meta")
        * @ORM\JoinColumn(name="imageid", referencedColumnName="id")
        * @ORM\Id
        */
        private $image;
    

    Note: Just relalised that nullable probably is not possible because that column is your ID column for the ImageMetadata but might not even be necessary if you define the realtion in this way.

    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP