Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete rows from join-table (ManyToMany) in Doctrine?
    text
    copied!<p>I have a join-table which is created by using <code>@ORM\ManyToMany</code> annotation in Symfony2/Doctrine. It joins <code>Category</code> and <code>Parameter</code> table.</p> <p>Now I want to delete all parameters from the Parameter table. Because there are foreign key constraints defined on join-table I can't just delete rows from Parameter table. First I have to delete child rows from join-table. But Dotrine's DQL syntax require to give a name of the entity, like:</p> <pre><code>DELETE Project\Entity\EntityName </code></pre> <p>But what is the name of the join-table entity generated by using ManyToMany association? How to deal with it?</p> <p>Alternately, how can I set ON UPDATE CASCADE and ON DELETE CASCADE on foreign key constraints in join-table defined by <code>@ORM\ManyToMany</code> annotation.</p> <p><strong>EDIT:</strong></p> <p>join-table schema:</p> <pre><code>CREATE TABLE `categories_params` ( `category_id` INT(11) NOT NULL, `param_id` INT(11) NOT NULL, PRIMARY KEY (`category_id`, `param_id`), INDEX `IDX_87A730CB12469DE2` (`category_id`), INDEX `IDX_87A730CB5647C863` (`param_id`), CONSTRAINT `categories_params_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `allegro_category` (`id`), CONSTRAINT `categories_params_ibfk_2` FOREIGN KEY (`param_id`) REFERENCES `category_param` (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB; </code></pre> <p><code>on UPDATE</code> and <code>on DELETE</code> by default are set to <code>RESTRICT</code></p> <p><strong>THE FINAL SOLUTION WOULD BE:</strong></p> <pre><code> * @ORM\ManyToMany(targetEntity="CategoryParam", cascade={"persist","remove"}) * @ORM\JoinTable(name="categories_params", * joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE")}, * inverseJoinColumns={@ORM\JoinColumn(name="param_id", referencedColumnName="id", onDelete="CASCADE")}) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload