Reputation: 7935
I want to do mapping that does this:
User can own multiple Games. Games can have multiple owners.
I have id
column in game
table and game
and user
columns in ownership
table. How I can connect these fields? I want to have game
and user
fields in ownership
related to user
and game
tables.
I tried OneToMany
and ManyToMany
, but the first one results in generating additional columns. I don't want to insert anything in game
table.
--edit-- My @ManyToMany code:
/**
* @ORM\ManyToMany(targetEntity="Ownership")
* @ORM\JoinTable(name="ownership",
* joinColumns={JoinColumn(name="user", referencedColumnName="id")},
* inverseJoinColumns={JoinColumn(name="game", referencedColumnName="id")}
* )
*/
It causes an error in Symfony's command line:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] Couldn't find constant JoinColumn, property GameShelf\Us
ersBundle\Entity\User::$ownership.
Upvotes: 0
Views: 620
Reputation: 11374
Of course you need many to many relations if, like you said:
User can own multiple Games. Games can have multiple owners.
The Doctrine should create third table (which could contains only two foreign keys: game_id and ownership_id).
The error is caused because Doctrine dosen't know what JoinColumn
is. You just did wrong annotation because you forgot (again! ;)) precede JoinColumn
with `@ORM. The right annotation should look like this:
/**
* @ORM\ManyToMany(targetEntity="Ownership")
* @ORM\JoinTable(name="ownership",
* joinColumns={@ORM\JoinColumn(name="user", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="game", referencedColumnName="id")}
* )
*/
Upvotes: 2
Reputation: 712
Your description of what you want doesn't fit at all. On the one hand you want to have a relation between only Users and Games, and only between Games and Owners.
But on the other hand you say that Users and Owners are related in some way, but Games and Users aren't.
You should read through http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-unidirectional and try to find what you really need. I would recommend a ManyToMany relation with a join table. Thus you don't have to deal with additional fields in your tables
Upvotes: 0
Reputation: 1057
Sounds like you need to introduce a new entity (something like OwnedGame) that holds the connection between a Game and an Owner (and possibly its own properties like when it was bought and such).
Then OwnedGame would be ManyToOne with Game and with Owner, and neither Game nor Owner would need to include a new column.
Upvotes: 0