Basically, I have two tables (Article and Tag) and I want to make many-to-many (one article can have many tags, one tag can be assigned to many articles) relation with some extra attributes. I can write this in Doctrine2 by breaking it into two separate relations (one-to-many, many-to-one) and one relation table ArticleTag with my extra attributes.
My problem is that I don't know if I can make Doctrine2 to create also the join table entities for me. What I mean is when I call:
$article = /* create new article, etc... */
$tag = /* create new tag, etc... */
$article->addTag($tag);
$em->persist($article);
$em->flush();
It DOES create both Article and Tag entities in the database but it DOES NOT create ArticleTag entity (in other words, it doesn't create the connection between Article and Tag). I could create it on my own but I would rather rely on Doctrine2.
Of course, it works fine when I use standard join table generated by Doctrine2 but I need those extra attributes.
Does anyone have any idea or do I really have to do it manually?
EDIT: source codes
/**
* @ORM\Entity
*/
class Article {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Tag", mappedBy="article", cascade={"persist"})
* @ORM\JoinTable(name="ArticleTag", joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")})
* )
*/
protected $tags;
...
}
/**
* @ORM\Entity
*/
class ArticleTag {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Article")
*/
private $article;
/**
* @ORM\ManyToOne(targetEntity="Tag")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
private $tag;
/**
* @ORM\Column(type="float")
*/
protected $priority = 0.5;
}
/**
* @ORM\Entity
*/
class Tag {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=32)
*/
protected $name;
}