【问题标题】:How to update sfUserProfile after the completion of a separate module? postSave() not working单独模块完成后如何更新 sfUserProfile? postSave()不工作
【发布时间】:2011-09-05 14:33:07
【问题描述】:

我有一个包含 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 更新配置文件。

建议?

更新 - 每个请求,这里是请求的信息:(我的 >>lib>form>>doctrine>>CorporationForm.class.php 是空的......我在操作类中尝试我的函数......这可能成为问题)。澄清一下,我只需要在用户完成公司模块后使用新创建的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 }

【问题讨论】:

    标签: symfony1 doctrine symfony-1.4 dql


    【解决方案1】:

    在您的表单中:

    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');
    

    【讨论】:

    • 我只是一个初学者(如果你还不知道的话)...你能详细说明一下吗?我是否将该函数放在我的操作类或公司模块的表类中?那么,我什么时候调用该函数,以及调用什么?我是保留我的 postsave() 方法还是完全摆脱它?感谢您对我的耐心...
    • 在您的表单 lib/form/doctrine/NameForm.class.php 中使用 public function doUpdateObject($values) 您可以更新表格中的一些数据。您可以发布代码吗你的形式,架构?some example
    • denys281:感谢您的回复;我失去了希望。我已经用您要求的信息更新了我的问题。 ...再次感谢您帮助这个新手。 ...它开始点击。 :)
    • Ок,您希望在用户发布表单后,更新 sfGuardUserProfile 中的 Corporate_id 值?
    • 这很简单;-) 你的应用程序/前端/模块/你的模块中有操作类..更新我的答案。
    【解决方案2】:

    别忘了 ->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');      
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2020-02-15
      相关资源
      最近更新 更多