ORM Doctrine ManyToOne on update CASCADE (Symfony)

前端 未结 1 803
难免孤独
难免孤独 2021-02-08 18:00

I have two entities

class Promotor
{

/**
 * @ORM\\ManyToOne(targetEntity=\"Ciudad\", inversedBy=\"promotor\")
 * @ORM\\JoinColumn(name=\"ciudad_id\", referenced         


        
1条回答
  •  名媛妹妹
    2021-02-08 18:30

    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: http://docs.doctrine-project.org/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;
    

    0 讨论(0)
提交回复
热议问题