tags:

views:

37

answers:

1

I'm generating models from my Mysql db. It generates a foreign key collection properly, but not the other way around... Is this supposed to be 'by-design', or am i doing something wrong?

pseudo code alert

User:
  UserId pk
  LocationId fk     //User location

Location
  LocationId pk
  UserId fk         //Location owner

Generated code:

class User() {
    hasMany('Location') //for locations owned by the user

    //BUT NOT THIS ONE:
    //hasOne('Location_1') //for current location of user
}

class Location() {
    hasMany('User') //for users which are on that location

    //AND NOT THIS ONE
    //hasOne('User_1') //for location owner
}
+1  A: 

You need to define an association table. Your pseudo code is misleading – in many to many relationship, no foreign keys are used in User or Location classes, but in association class.

See PDF manual page 76.

jholster
No, the code is right. This doesn't need an association class, it needs a one-to-many relationship TWO times.
Ropstah
User->locationid is updated whenever the location of a user changes. When a Location is inserted, a User is assigned as it's owner.
Ropstah
Sorry, I misinterpreted your question. Just a guess, but maybe you should explicitly define _owningSide_? http://www.doctrine-project.org/documentation/manual/1_2/en/defining-models#relationships
jholster
I'm really missing something somewhere. Please correct me but: let's say there is one foreign key relation where User.LocationId links to Location.Id. This means Doctrine will generate Location->hasMany('User'). But doesn't this imply there should be a User->hasOne('Location') generated also?
Ropstah
I wouldn't count on that, since the manual says: _"Generating from existing databases is only meant to be a convenience for getting started. After you generate from the database you will have to tweak it and clean things up as needed."_ http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-models#generating-models
jholster
Ah ok, no problem, but I can just enter the hasOne for the other table and everything should be fine then? :)
Ropstah
Yes, you can (should) fine-tune your automatically generated classes to fit your needs.
jholster