I have two entities for example:
class Dog
{
/**
* @var House
*
* @ORM\ManyToOne(targetEntity="House")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="house_id", referencedColumnName="id")
* })
*/
private $house;
}
class House
{
/**
* @var ArrayCollection|null
* @ORM\ManyToMany(targetEntity="Dog",cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="dog_id", referencedColumnName="id", nullable=true)
* })
*/
protected $dog;
}
I need to throw an event if field house in Entity Dog was update (set or remove) then add or remove field dog in Entity House.
Can anyone show me how do this ?
Doctrine will do this for you but depending on the cascade option. But your annotations are not correct. In the Dog entity you have annotation for a ManyToOne and in the House entity for a ManyToMany relation. But you should choose between
Take a look into the Doctrine's association mapping to read about all the types of associations and how to define them.
If you are using Symfony (4 or 5) you should use the commandline make tool to add properties and methods with all the annotations, even for relations.
bin/console make:entity DogType
relationwhen asked for the Field type and you will have to answer some additional questions.