views:

80

answers:

2

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 forign 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

  1. Is my assumption correct?

  2. If so, has the situation changed in Doctrine 2?

  3. If the answers to either of the above are true, how do you configure the models so that all the columns "line up"

+1  A: 

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:
Goran Jurić
I'm not jumping into YAML immediately to avoid the whole "too many new things at once" mental problem. What sort of classes/setup methods does the above generate?
Alan Storm
+1  A: 

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.

DrColossos
I did generate my models from the DB, but it didn't pick up the relationships. Is there a special option you need to pass into the generateModelsFromDb method to tell doctrine to take its best guess at relationships?
Alan Storm
No, it should be fine when you generate it. Have you tested the generated models in your application?
DrColossos
Ah, I think I see what you were getting at here. In case it wasn't clear for others, aliasing the database column names to what doctrine wanted for field names did the trick ($this->hasColumn('fksection_id as section_id')
Alan Storm