'Set @JoinColumn name in doctrine
EDIT:
In the end as @Jakumi commented it was just a cache issue, after clearing it everything worked as expected.
There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
In the project I'm working on we use doctrine as ORM only meaning we don't use doctrine migrations. I'm trying to map a column (from an existing table) which has a OneToMany association like this:
/**
* @var InternalUser
* @ORM\ManyToOne(targetEntity="App\Entity\InternalUser", inversedBy="blocos")
* @ORM\JoinColumn(nullable=false, name="owner")
*/
private $owner;
The problem is the column name is only owner
not owner_id
but when querying the database, using ->find('id')
for example, doctrine appends an _id
suffix to the column name, generating a query that looks like this:
SELECT .... t0.owner_id AS owner_id_6 FROM ...
And when the query is executed I get a Undefined column: 7 ERROR: column t0.owner_id does not exist
because the right name is just 'owner'.
I've tried setting both name
and referencedColumnName
to "owner"
in the @JoinColumn annotation but it appends the _id
suffix anyway.
I've also looked at naming strategies but it doesn't seem to be responsible for the _id
suffix (I'm using the default UnderscoreNamingStrategy
that comes with symfony4)
Is there anyway to configure doctrine to not add the '_id' suffix?
PS:
Changing the column name is not an option
Solution 1:[1]
name
is exactly for that, when you use it, doctrine should map correctly.
Now, what it seems awkward here is that you mentioned ->find('id')
.
If you have table A, and you desire to look something based on the id
of that table is when you use ->find($id)
, it will look for the id of that table.
But you are talking of a relation, in which case you should use findBy(['field' => $value])
or findOneBy(['field' => $value])
.
I hoe this help, if not, can you show the exact query you are using and both side of the relation?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richard PĂ©rez |