如何在完成单独的模块后更新sfUserProfile? postSave()不起作用

时间:2011-09-05 14:33:07

标签: symfony1 doctrine symfony-1.4 dql

我有一个包含sfRegistration和sfProfile的注册表单,在完成后,将其重定向到另一个表单 - 以确定用户的公司并且是一个单独的模块。 sfProfile包括来自公司模块的字段...... corporate_id。但由于corporate_id不是个人资料的一部分,因此在注册时不包括在内。在完成公司模块之后,问题是,使用新完成的公司模块的corporate_id更新用户配置文件的最佳方法是什么?我试过这个:

public function postSave()
{
$corpId = $this->form->getId();
$logged_user_id = sfContext::getInstance()->getUser()->getId();
Doctrine_Query::create()
->update('sf_guard_user_profile p')
->set('p.corporate_id', $corpId)
->where('p.user_id' == $logged_user_id )
->execute();
}

置于公司模块的操作中,但它没有使用company_id更新配置文件。

建议?

更新 - 每个请求,这里是请求的信息:(我的>>>格式>> doctrine>> CorporationForm.class.php是空的...我在actions类中尝试我的函数。 ..这可能是问题)。为了澄清,我只需要在用户完成公司模块后用新创建的corporate_id更新用户的配置文件。
我的架构:

sfGuardUser:
  actAs: [Timestampable]
  columns:
    first_name: string(255)
    last_name: string(255)
    email_address:
      type: string(255)
      notnull: true
      unique: true
    username:
      type: string(128)
      notnull: true
      unique: true
    algorithm:
      type: string(128)
      default: sha1
      notnull: true
    salt: string(128)
    password: string(128)
    is_active:
      type: boolean
      default: 1
    is_super_admin:
      type: boolean
      default: false
    last_login:
      type: timestamp
  indexes:
    is_active_idx:
      fields: [is_active]
  relations:
    Groups:
      class: sfGuardGroup
      local: user_id
      foreign: group_id
      refClass: sfGuardUserGroup
      foreignAlias: Users
    Permissions:
      class: sfGuardPermission
      local: user_id
      foreign: permission_id
      refClass: sfGuardUserPermission
      foreignAlias: Users

sfGuardUserProfile:
 actAs:            { Timestampable: ~ }
 columns:
  id: { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }
  user_id: { type: integer }
  corporate_id: { type: integer }
  type_id: { type: integer, notnull: true }
  prefix_id: { type: integer }
  middle_name: { type: string(55) }
  suffix_id: { type: integer }
  phone: { type: string(55), notnull: true }
  home_address_line_one: { type: string }
  home_address_line_two: { type: string }
  home_city: { type: string }
  state_id: { type: integer }
  home_zip: { type: integer } 
 relations:
  User: { class: sfGuardUser, local: user_id, foreign: id, type: one, foreignType: one,     foreignAlias: Profile }
  Type: { local: type_id, foreign: id, type: one, foreignAlias: Types } 
  Prefix: { local: prefix_id, foreign: id, type: one, foreignAlias: Prefixs } 
  Suffix: { local: suffix_id, foreign: id, type: one, foreignAlias: Suffixs }   
  State:  { local: state_id, foreign: id, foreignAlias: States }  
  Corporation: { local: corporate_id, foreign: id, foreignAlias: Corporations }

Corporation:
  columns:
   id:             { type: integer, primary: true, notnull: true, autoincrement: true,    unique: true }
   user_id:        { type: integer }
   name:           { type: string(55), notnull: true }
   address_line1:   { type: string(255), notnull: true }
   address_line2:   { type: string(255), notnull: true }
   city:           { type: string(25), notnull: true }
   state_id:       { type: integer, notnull: true }
   zip:            { type: string(25), notnull: true }
   phone:          { type: string(25), notnull: true }
   email:          { type: string(100), notnull: true }
   website:        { type: string(100) }
   logo:           { type: string(255) }
  relations:
    User: { class: sfGuardUser, local: user_id, foreign: id, foreignAlias: Users }

2 个答案:

答案 0 :(得分:0)

以您的形式:

public function  doUpdateObject($values)
  {

   parent::doUpdateObject($values);
   $this->getObject()->setCorporateId(your value);

}

例如 //apps/frontend/yourmodule/action.class.php

protected function processForm(sfWebRequest $request, sfForm $form)
    {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {

       $corporation = $form->save();
       $user_id=$this->getUser()->getGuardUser()->getId();
       $profile = Doctrine_Core::getTable('sfGuardUserProfile')->findOneByUserId($user_id);
       $profile->setCorporateId('your value');

答案 1 :(得分:0)

不要忘记 - > save();

    public function setId() 
  {
        $user_id = $this->getUser()->getGuardUser()->getId();
        $corp_id = 1;
        $user_profile = Doctrine_Core::getTable('sfGuardUserProfile')->findOneByUserId($user_id);
        $user_profile->setCorporateId($corp_id)->save();
   }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {            
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        self::setId();        
        $corporation = $form->save();    
        $this->redirect('dashboard/index');      
    }
  }