Relation one to one in sf1.4/propel

2019-08-28 22:37发布

问题:

i have installed sfGuardPlugin and created this model:

propel:
  sf_guard_user_profile:
    _attributes:       { phpName: sfGuardUserProfile }
    id:                ~
    user_id:           { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade }
    name:              varchar(50)

As it is written here, http://www.propelorm.org/wiki/Documentation/1.4/Relationships (see "One-to-one relationships"), It is supposed symfony generates the function sfGuardUser->getSfGuardUserProfile() and sfGuardUserProfile->getSfGuardUser() but I have this code:

  // this works
  $c1 =  new Criteria();  
  $elements = sfGuardUserProfilePeer::doSelect($c1);
  var_dump($elements[0]->getSfGuardUser());

  // this doesn't work
  $c2 = new Criteria();
  $elements = sfGuardUserPeer::doSelect($c2);
  var_dump($elements[0]->getSfGuardUserProfile());

and it doesn't work. It says:

Call to undefined method BasesfGuardUser::getSfGuardUserProfile

sf 1.4/propel 1.4

Javier

回答1:

the user_id field in the sfGuardProfile should be a primary key, so propel will see it as a one-to-one relation.

propel:
  sf_guard_user_profile:
    _attributes:       { phpName: sfGuardUserProfile }
    user_id:           { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade, primary: true }
    name:              varchar(50)

as your sfGuardUserProfile is a one-to-many relation with your sfGuardUser, so the getSfGuardUserProfile() method doesn't exist, the method that does exist is sfGuardUserProfiles() (the only difference is an 's' in the method name, and it will result an array of user profile)

ps: sorry for my bad english :D