How do I setup a join table with extra columns, or a many-to-many association with additional properties, in Doctrine 2?
-
@ThatBlairGuy I noticed you are removing a lot of the extra tag. This edit was adequate, but please be sure to fix all aspects of a post in addition to removing the tag. For more, read this meta discussion– ryanyuyuJan 13, 2016 at 21:22
2 Answers
First off, let me explain that this does not exist:
A join table (also known as a junction table or cross-reference table) is a table that links 2 (or more) other tables together within the same database by primary key. This means that a join table will only contain foreign keys, there is no place for these extra columns.
So when you need extra columns in such a table, it is no longer just a "link" between other tables, but becomes a real table on its own!
In terms of Doctrine 2, you no longer have a many-to-many association between 2 entities, but get a one-to-many/many-to-one association between 3 entities.
Continue reading here for more detailed explanations:
-
3Links are updated! (Formatting may be a bit off, will be fixed asap) Jul 3, 2014 at 14:31
-
-
@JasperN.Brouwer Congratulations, very well written! Thanks for your links.– fabpicoMar 11, 2016 at 14:38
-
In the second article, I suggest a minor update. Instead of the full event, use a LifecycleCallback within the entity itself:
/**
* @ORM\Entity
* @ORM\Table(name="jobs”)
* @ORM\HasLifecycleCallbacks
*/
class Job
{
// ...
/**
* @ORM\PreRemove
*/
public function preRemoveCallback()
{
$this->setPerson(null);
$this->setCompany(null);
}
}