问题
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