How to use Onupdate=Cascade in Doctrine 2

Asked

Viewed 218 times

1

Personal I have a relationship of much for one in a table and I need to put the Onupdate in Cascade as I do because I try so of the error:

/**
 * @ORM\ManyToOne(targetEntity="JN\Entity\Planos\Plano")
 * @ORM\JoinColumn(name="plano_id", referencedColumnName="id", nullable=true, onDelete="SET NULL", onUpdate="CASCADE")
 **/
private $plano;

Thank you

  • John, can you tell me why you want this kind of behavior? Thanks.

  • @Rodrigorigotti for the following I always use onUpdate=Cascade, to maintain the integrity of the keys when making a change in a table that by conseguencia has others linked to it. No Doctrine was the first ORM I saw that left the onDelete and disappeared with onUpdate. I’ve always worked like this and I don’t know any other way to maintain data integrity when there are relationships other than this.

  • If you need an onUpdate to maintain referential integrity, it means something in your bank is modeled wrong. You could post an image of your data model in the question?

  • @Rodrigorigotti think of a client table and a neighborhood table for example. Client table I have the following columns: ID, Name,id_neighborhood. and In the table neighborhoods I have the columns ID, Neighborhood Name.I have a relationship and 1 neighborhood for several clients. Suppose the center neighborhood is ID 1 today and in a month for some reason it changes to 4. If you have Onupdate you will be changed automatically otherwise only manually. Or is there another way?

  • You want to change the neighborhood id and thereby want the foreign table keys bairros on the table clientes, that’s it?

  • This would be one of the simplest examples. But this would be the thought.

  • It is not correct to change the id of a row in a table, since when this id is assigned to a row it cannot be changed. The right thing is, if you want to change a person’s neighborhood, pick it up with the method findOneBy of EntityRepository and assign it to the user with $user->setBairro($bairro).

  • Okay. I get it. I’ll change then. Thank you very much for the explanation.

Show 3 more comments

1 answer

2


  • kabstergo I had seen this in the documentation, but I still had a little bit of hope left. Because I always use on On Delete and On Update to control the movements derived from the tables. Here is my disappointment with this point of Doctrine. How is the right way to work with these situations when you have? Thank you

  • @Joaonivaldo here is the request and discussion about the http://www.doctrine-project.org/jira/browse/DC-1628 resource apparently the developers thought that there is no practical application in which the value of an identifier would be updated (Primary Keys) the claim is that this would not be the burden of the ORM library. Then the "correct" way (according to them) would perform the operation outside the application, through the database since it supports the cascading update of these identifiers.

  • I fully agree that this is a task for the database and not for the ORM. But if there is no onUpdate option in annotate when I do an update in the classes and run Orm:schema-tool:update it altomatically puts onUpdate in restrict ai I go in Mysql and change it to Cascade then I change the classes and forget the change there the result is problem in certain. You can make Doctrine not change the onUpdate that is in MYSQL when I run the command to actualize the tables?

  • @Joaonivaldo hmm understood your problem, I will research about it if I find something I updated the answer.

  • thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.