I'm working with an existing database schema, and trying to setup two Doctrine models with a Many to Many relationship, as described in this document
When creating tables from scratch, I have no trouble getting this working. However, the existing join tables use a different naming convention that what's described in the Doctrine document. Specifically
Table 1
--------------------------------------------------
table_1_id
....other columns....
Table 2
--------------------------------------------------
table_2_id
....other columns....
Join Table
--------------------------------------------------
fktable1_id
fktable_2_id
Basically, the previous developers prefaced all foreign keys with an fk
.
From the examples I've seen and some brief experimenting with code, it appears that Doctrine 1.2 requires that the join table use the same column names as the tables it's joining in
Is my assumption correct?
If so, has the situation changed in Doctrine 2?
If the answers to either of the above are true, how do you configure the models so that all the columns "line up"
You just have to set the local and foreign parts to match your column names
Table1:
columns:
table_1_id: int
....
relations:
Table2:
foreignAlias: FromSecond
local: table_1_id
foreign: fktable1_id
refClass: JoinTable
Table2:
columns:
table_2_id: int
....
relations:
Table1:
foreignAlias: FromFirst
local: table_2_id
foreign: fktable2_id
refClass: JoinTable
JoinTable:
columns:
fktable1_id: int
fktable2_id: int
relations:
Table1:
Table2:
Take a look at the docs regarding N:M relations.
When you look at
$this->hasColumn('user_id', 'integer', null, array(
'primary' => true
)
);
from the docs, you see that is is up to you how you name the column in your join model.
Since you have an existing database, why not generate the models via Doctrine ? The API shows you the needed options for this. I used that myself and it works pretty good. Sometimes you may need some cleanups if you have a compliacted structure but the generated files are a very good start.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.