Unidirectional ManyToMany : rename "id" field #10004
-
Hello, I'm trying to create a The problem is that my Here's the field of my class Question {
...
#[ORM\ManyToMany(targetEntity: Tag::class, fetch: 'EAGER')]
private $tags;
...
} It looks like Doctrine is looking for a primary key named "id" specifically instead of just using the first primary key. I don't know how to change that. I've played a lot with I come from less advanced ORMs and manual SQL, hence my difficulty to grasp high-level concepts like these. Thanks a lot for any help ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
TLDR Answer : Well, after spending the last afternoon on this question, I didn't expect to find an answer this quickly. An error message suggested that Doctrine makes a clear distinction between fields (which map to columns) and relations (which don't necessarily have a column, at least not on the same table). So I actually defined a dummy field to act as my reverse relation on the #[ORM\ManyToMany(targetEntity: Question::class, mappedBy: 'tags')]
#[ORM\JoinColumn(referencedColumnName: 'slug')]
private $questions; Then, I stumbled upon this StacOverflow answer with a promising piece of code : * @ORM\JoinTable(
* name="table1_table2",
* joinColumns={
* @ORM\JoinColumn(name="foo_uid", referencedColumnName="uniqueIdentifier")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="bar_uid", referencedColumnName="uniqueIdentifier")
* }
* )
*/ I only needed this |
Beta Was this translation helpful? Give feedback.
TLDR Answer :
#[InverseJoinColumn(referencedColumnName: 'slug')]
Well, after spending the last afternoon on this question, I didn't expect to find an answer this quickly. An error message suggested that Doctrine makes a clear distinction between fields (which map to columns) and relations (which don't necessarily have a column, at least not on the same table). So I actually defined a dummy field to act as my reverse relation on the
Tag
end. It looks like this :Then, I stumbled upon this StacOverflow answer with a promising piece of code :