I'm building my first application with symfony2. I want to create this database layout with entities.
# document
document_id | INT | AI | PK
document_name | STRING
services | RELATION MANY2MANY
...
# service
service_id | INT | AI | PK
service_name | STRING
...
*# _table_document_service*
document_id | INT
service_id | INT
amount | INT | DEFAULT 1
It's not a problem to create the entities document and service. To create the relation between document and service I will use the ManyToMany-Relation like this one:
/**
* @ORM\ManyToMany(targetEntity="Services")
* @ORM\JoinTable(name="_table_document_service",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")}
* )
*/
private $services;
But I want that in the join table there should also be a column called amount. How can i do that?
Considering that you want to add extra fields on a relation, then it's not a relation anymore but clearly -"designly" speaking- an association object.
Hence, what you want in your "table document service" table is something like that
Document ------- Table document service -------- Service
Doing so means 2 things :
Many to one associations between Document ---- Table document service and Table document service -------- Service
A sort of "Composite key" in the Table document service entity based on these associations (your primarykey then becomes a composite of the relations between these objects.
Here are some examples : as you can see in the official doctrine 2 documentation you can do assotiations based on manytoone with composite keys as follow.
EDIT : here is some bunch of code, trying to follow your example.
I can add that, I guess, you can add too another OneToMany association in the Service class as the doc did for the Document entity, something like