如何保存额外的实体,同时坚持另一个原则? [英] How to save additional entity while persisting another in Doctrine?

查看:100
本文介绍了如何保存额外的实体,同时坚持另一个原则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个地方实体和一个距离一个,像这样:

I've got a Place entity and a Distance one, like so:

class Place
{
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
    private $id;

    /** @ORM\Column(type="string", length=62, nullable=false) */
    private $name;

    /** @ORM\OneToMany(targetEntity="Distance", mappedBy="origin") */
    protected $distancesTo;

    /** @ORM\OneToMany(targetEntity="Distance", mappedBy="destination") */
    protected $distancesFrom;
}

class Distance
{
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
    private $id;

    /** @ORM\ManyToOne(targetEntity="Place", inversedBy="distancesTo") */
    protected $origin;

    /** @ORM\ManyToOne(targetEntity="Place", inversedBy="distancesFrom") */
    protected $destination;

    /** @ORM\Column(type="integer") */
    private $miles;
}

我想要每次新的距离(从Place_A到Place_B)被保存,反向距离(从Place_B到Place_A)也被插入到DB中。如何做到这一点?

I want that every time a new Distance (from Place_A to Place_B) is saved, the reverse distance (from Place_B to Place_A) gets inserted too in the DB. How can I do that?

推荐答案

你需要创建监听器,并且继续坚持 code>实体。虽然它仍然存在,您可以使用反向路线创建新的距离

You need to create listener and listen for persisting on Distance entity. While it persists you can create new Distance with reverse route.

这篇关于如何保存额外的实体,同时坚持另一个原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆