Reinherd
Reinherd

Reputation: 5506

Should I create many to many relationship?

I'm stuck ATM. Months passed since I did the latest website development, and I'm feeling stupid now.

I've created in Doctrine2 a class named User, and one other class named Publication.

Publication class has a ManyToOne relationship with User, so I can identify which User created that Publication.

Like that:

/**
 * @ORM\ManyToOne(targetEntity="Users")
 * @ORM\JoinColumn(name="user", referencedColumnName="id")
 */
private $user;

Now, what I want to do, is create a Many To Many (I guess) relationship between those two classes to do the following:

In Mysql Schema, I think I should end up with a 3rd table called something like User_Voted_Publication however I'm not sure how to do that in Doctrine.

If I just create a ManyToMany relationship, I can't add the attribute points which will hold the number of how many points did any User give to any specific Publication.

Any tips are appreciated.

Thank you so much, and sorry if this question seems to be stupid. I already feel like that =).

Upvotes: 0

Views: 37

Answers (1)

Sehael
Sehael

Reputation: 3736

In my experience (anyone feel free to chime in if I am wrong), a many-to-many relationship in doctrine only allows for the use of the primary keys (in your case, the user_id and publication_id). If there are any other fields in the table (the User_Voted_Publication table), doctrine won't recognize them. What I have done to get around this is create the new entity (User_Voted_Publication), and then create a one-to-many relationship with both other tables (user and publication tables).

This will allow you to add any other field in the User_Voted_Publication table that you want. One thing that I noticed that may not be ideal... I had to create an auto-increment id field in the table instead of using the natural primary key(user_id, publication_id), but it still works fine. I just made a unique constraint on the two fields(user_id, publication_id).

Upvotes: 1

Related Questions