How to save additional entity while persisting another in Doctrine?

这一生的挚爱 提交于 2019-12-11 13:10:33

问题


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;
}

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?


回答1:


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



来源:https://stackoverflow.com/questions/30569463/how-to-save-additional-entity-while-persisting-another-in-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!