I have two entities
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false)
*/
protected $ciudad;
and
class Ciudad
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=50)
*/
private $nombre;
A "Promotor" can live in one "Ciudad" (City). And in a "Ciudad" (City) can live many "Promotores".
If i add onDelete="CASCADE" in JoinColumn
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
protected $ciudad;
it generate the next code
ALTER TABLE promotor DROP FOREIGN KEY FK_BF20A37FE8608214;
ALTER TABLE promotor ADD CONSTRAINT FK_BF20A37FE8608214 FOREIGN KEY (ciudad_id)
REFERENCES Ciudad (id) ON DELETE CASCADE
but also i like do CASCADE on update. I try with onUpdate="CASCADE" but it doesn' work
[Doctrine\Common\Annotations\AnnotationException]
[Creation Error] The annotation @ORM\JoinColumn declared on property Web\PromotorBundle\Entity\Promotor::$ciudad does not have a property named
"onUpdate". Available properties: name, referencedColumnName, unique, nulla
ble, onDelete, columnDefinition, fieldName
By the error I understand that the property onUpdate does not exist, but.. Is there any way to do cascade on update?
The onDelete="CASCADE" is used on the database level. As you already said there is no onUpdate. Another downside is that ON DELETE CASCADE only works on InnoDB. It doesn't work on MyISAM.
But you can use Doctrine in-memory cascade operations:
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false)
*/
protected $ciudad;
It's all described in the documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations
Plus you can skip the JoinColumn annotation, because the way you have it written, is the default configuration and it's generated implicitly.
So you can just write:
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor", cascade={"persist", "remove"})
*/
protected $ciudad;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With