drzyeetvt41077335 2017-01-02 05:07
浏览 36
已采纳

在Symfony中插入父实例的子实体

I have a parent entity which is referring to a child entity and the classes are written as follows:

class MyEntity {
  /**
  * ORM Annotations
  */
  private $id;

  /**
  * ORM Annotations
  */
  private $name;

  /**
  * @var string
  * @ORM\OneToOne(targetEntity="Picture")
  */
  private $image;

  /** Getters & Setters **/
}

private Image {
  private $id;
  private $image_url;
}

Here, Image is a weak entity and I don't want to insert image before inserting MyEntity object. Basically, my question is, how can I render the form for MyEntity so that, Image form appears as a part of it, and image get saved when I save MyEntity.

  • 写回答

1条回答 默认 最新

  • drzfz9995 2017-01-02 07:04
    关注

    How come your code has targetEntity="Picture" and you have defined Image as child Entity. I guess there is a typo, you need to correct.

    You need to add cascade={"persist", "update"} to your image association in MyEntity.

    This will make sure, Image entity being created / updated along with MyEntity.

    Now the form Part

    Create a new FormType for Image, lets call it as ImageType. In your MyEntity FormType, add the new FormType as a new field :

    $builder
        ->add('image', ImageType::class, array(
            'label' => 'Image'
    ));
    

    Now on form Submission, The child entity (Image) will be created / updated accordingly.

    Note : You need to take care of image upload explicitly.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法