title: How to disable cascade deletion for many-to-many relations?
body: <p>For a Many-to-Many relations (say Groups and Users) the rows from the joint table are automatically removed as soon as either of the entities is deleted, like the cascade remove attribute was set for the relation. So basically I want to delete it <strong>IF ONLY IT IS EMPTY</strong>. So the solution must guarantee no relations were dropped (exactly like FK constraint guarantees it).</p>
  
  <p>Is it possible to not do that by default and throw an exception on foreign key constraint violation?</p>
  
  <p>PS: checking before deletion is not a solution since it's a race condition prone.</p>
  
  <p>PPS: mapping definitions are trivial, for the sake of completeness I post them here (even though they don't bring anything useful)</p>
  
  <p>PPPS: <code>onDelete: cascade</code> is not a solution either: it creates the corresponding <code>ON DELETE CASCADE</code> on the database level.</p>
  
  <p>PPPPS: <code>ON DELETE RESTRICT</code> <strong>CANNOT BE USED</strong> since doctrine will remove all the references from the joint table.</p>
  
  <p>In roles:</p>
  
  <pre><code>manyToMany:
      users:
          targetEntity: UserAccount
          mappedBy: roles
  </code></pre>
  
  <p>In users:</p>
  
  <pre><code>manyToMany:
      roles:
          targetEntity: Role
          joinTable:
              name: user_role
              joinColumns:
                  user_id:
                      referencedColumnName: id
              inverseJoinColumns:
                  role_id:
                      referencedColumnName: id
  </code></pre>