symfony1symfony-1.4sfguardsfdoctrineguard

Setting up the value of a field in a form taking from a Profile


I need some help here understanding how this works. See I'm using sfDoctrineGuardPlugin and add a Profile table as follow:

SfGuardUserProfile:
  connection: doctrine
  tableName: sf_guard_user_profile
  columns:
    id: { type: integer(8), primary: true }
    user_id: { type: integer(8), primary: false }
    idempresa: { type: integer(4), primary: false }
  relations:
    User:
      local: user_id
      class: sfGuardUser
      type: one
      foreignType: one
      foreignAlias: SfGuardUserProfile
      onDelete: CASCADE
      onUpdate: CASCADE

Now I need to setup a field value based on logged in user in SdrivingEmisorForm, for that I need to access to idempresa on sf_guard_user_profile but I don`t know how :-( I tried all this:

$user = sfContext::getInstance()->getUser();

echo $user->getGuardUser()->getProfile()->getIdempresa();
echo $this->getUser()->getGuardUser()->getProfile()->getIdempresa();
echo $user->getProfile()->getIdempresa();

and none works, which is the right way to access to profile fields based on my schema definition? Can any take a brief and explain a bit how I must understand this in order to no get the same doubt if things changes some day?

EDIT I've found the solution but using sfContext::getInstance() which many says is wrong, so in this case what is the right way to do this. Below th code works for me:

$user = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();

PS: I'm access from SdrivingEmisorForm class


Solution

  • You are defining the foreignAlias as SfGuardUserProfile, so you should use this name for the getter. In some action:

    $profile = $this->getUser()->getGuardUser()->getSfGuardUserProfile();
    

    In some template:

    $profile = $sf_user->getGuardUser()->getSfGuardUserProfile();
    

    Then:

    echo $profile->getIdempresa();