1

I'm new with symfony2, and I started a project of blog. This project has two entities, one of posts (Noticia) and one with comments (Comentario). I need to delete a post but I can't because of the relationship with comments.

Here is my attribute:

/**
 * @ORM\ManyToOne(targetEntity="Noticia", inversedBy="comentarios", onDelete="SET NULL")
 * @ORM\JoinColumn(name="noticia_id", referencedColumnName="id")
 */
private $noticia;

/**
 * @ORM\OneToMany(targetEntity="Comentario", mappedBy="noticia")
 */
private $comentarios = array();

I tried to put onDelete="SET NULL" but, when I updated my database with doctrine: php app/console doctrine:schema:update --force I got this error:

The annotation @ORM\ManyToOne declared on property Noticia 
Bundle\Entity\Comentario\::$noticia does not have a property named "onDelete".
Avaible properties: targetEntity, cascade, fetch, inversedBy
Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44
brander1990
  • 61
  • 1
  • 8

2 Answers2

8

I believe that the onDelete="SET NULL" should be part of the JoinColumn annotation, not the ManyToOne annotation.

thoufek
  • 331
  • 3
  • 4
0

One Way would be to search for all comments with the article relationship, you want to delete. So first delete all the comments, than the article itself. That a the eaysiest way without changing the annotation.

Jake94
  • 23
  • 4
  • I know, but, what is the "right way"?? Delete a post with a lot of comments is really a "NON best practicing"?? – brander1990 Jul 27 '15 at 00:06
  • 1
    I feel like onDelete="CASCADE" is appropriate here. If the Post is gone, do you want to keep the Comments? If not, allow the deletion of the Post to cascade to also delete its Comments. – thoufek Jul 27 '15 at 04:57