dumartinetj
dumartinetj

Reputation: 13

OneToOne Unidirectional cascade={"persist", "remove"} not working

On my symfony project I have a problem with 2 entities linked by a unidirectionnal OneToOne relationship. My entities are : Club and Address, a Club can have an address. See entity declaration bellow :

Club Entity

class Club{
/**
 * @ORM\OneToOne(targetEntity="FFPM\MainBundle\Entity\Address", cascade={"persist", "remove"}, orphanRemoval=true)
 * @ORM\JoinColumn(name="address_id", referencedColumnName="id", nullable=true)
 */
protected $address;

...

}

Address Entity

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

...

}

For some reason when I remove a Club the Address stays in database. I tried with orphanRemoval and cascade{"remove"} and I can't get it to work even if I'm pretty sure it's some simple mistake.

Upvotes: 1

Views: 1239

Answers (2)

Zakaria Charik
Zakaria Charik

Reputation: 119

The relation OneToOne unidirectionnal in doctrine manage only the one side since you don't complete the relation. so there is two way you can persist or remove address entity, is that you use the domain Event listener or you manage your entity manually. And unfortunately both cases are not good practice.

Upvotes: 1

Maxim Strutinskiy
Maxim Strutinskiy

Reputation: 531

Try use this construction:

     /**
     * @ORM\OneToOne(targetEntity="FFPM\MainBundle\Entity\Address", mappedBy="entidad", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="address_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     **/
    private $personaFisica;

Upvotes: 1

Related Questions