ML_
ML_

Reputation: 1010

One-to-one relations with sfGuardPlugin

I've added the following class in my schema.yml (I'm using symfony 1.4):

LoginKey:
  connection: doctrine
  tableName: sos_login_key
  options:
    type: InnoDB
    collate: utf8_unicode_ci
    charset: utf8
  columns:
    id:
      type: integer(8)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
      notnull: true
    user_id:
      notnull: true
      type: integer(8)
    keycode:
      notnull: true
      type: string(255)
    expires_at:
      type: datetime
      default: null
  relations:
    sfGuardUser:
      local: user_id
      foreign: id
      type: one

sfGuardUser:
  relations:
    LoginKey:
      local: id
      foreign: user_id
      type: one

Now when I try to do a JOIN on those tables in a query (sfGuardUser innerJoin LoginKey), the LoginKey records don't get hydrated; I see a new query is done for every row as I'm trying to access the LoginKey. What should I do?

Upvotes: 0

Views: 432

Answers (1)

1ed
1ed

Reputation: 3668

The owning side of the relation is the LoginKey so you have to define the relation only there. Add foreignType: one and remove the sfGuardUser part. So a correct schema looks something like this:

LoginKey:
  columns:
    user_id:
      type: integer
      notnull: true
    keycode:
      type: string(255)
      notnull: true
    expires_at:
      type: datetime
      default: null
  relations:
    sfGuardUser:
      local: user_id
      foreign: id
      type: one
      foreignType: one

And as a side note if you want to extend the schema of a plugin you have to use the package parameter (but in your case it is not required as no new fields have to be added to sfGuardUser)

sfGuardUser:
  package:
    sfDoctrineGuardPlugin.lib.model.doctrine
  relations:...

When not using this the classes will be generated into a wrong location and will conflict with others so you have to remove those classes. Try php symfony doctrine:clean and check if it's removed all unused classes (sfGuardUser) from lib/model and lib/model/base.

Upvotes: 1

Related Questions