【问题标题】:How to create a one-to-one related record in a separate table during processing (save()) of a form?如何在处理(保存())表单期间在单独的表中创建一对一的相关记录?
【发布时间】:2011-09-12 19:27:19
【问题描述】:

这是架构:

sf_guard_user
  columns:
    id              { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }
    username            { type: string }
    firstname           { type: string }
    lastname            { type: string }
    password            { type: string }
    salt, algorith, etc...

sf_guard_user_profile
  columns:
    id      { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }
    user_id     { type: integer }
    user_type   { type: integer }
  relations:
    User: { class: sfGuardUser, local: user_id, foreign: id, type: one, foreignType: one, foreignAlias: Profile }
    Type: { local: type_id, foreign

这就是我在前端尝试做的事情:我试图让前端用户创建新用户的......我可以做到并且一切顺利。

我卡在哪里:(1)在创建sf_guard_user的save()过程中,创建新用户的sf_guard_user_profile并将'user_id'的列值设置为新创建的sf_guard_user的主键(列' ID')。 (2) 然后还将列'user_type'设置为4。

我什至不知道从哪里开始。如果有人能指出我正确的方向,我将不胜感激。

更新:

这是我的 actions.class.php 文件(项目/源文件/apps/modules/users/actions):

protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sf_guard_user = $form->save();      
      $this->redirect('users/edit?id='.$sf_guard_user->getId());
    }
}

【问题讨论】:

    标签: symfony1 doctrine symfony-1.4 dql


    【解决方案1】:

    看来你理解的不对。当您正确配置 sfGuardPlugin 时,它会负责创建配置文件。

    让我们一步一步回顾这个过程:

    1) 首先,正确配置 sfGuardPlugin,将其添加到您的 app.yml

    all:
      sf_guard_plugin:
        profile_class:      yourProfileClass
        profile_field_name: user_id
    

    2) 使用表单保存您的用户:

    $form->save();
    

    3) 检索新创建的配置文件,并对其进行修改:

    $user = $form->getObject();
    $profile = $user->getProfile();
    $profile->setUserType(4);
    $profile->save();
    

    4) 你完成了!我在我的一个网站上使用了完全相同的方法。所以我和我的用户 100% 确定这种方法有效。请记住正确设置 sfGuardPlugin,即使您不使用它的管理生成器,它也会创建配置文件;

    【讨论】:

    • 只是一个注意事项:在设置类型之前不会自动创建配置文件...
    【解决方案2】:

    如果配置正确,sfGuardPlugin 会自动为您创建配置文件。在你的 app.yml 中添加这段代码:

    all:
      sf_guard_plugin:
        profile_class:      sf_guard_user_profile
        profile_field_name: user_id
    

    这将配置插件,以便在创建新用户时创建新配置文件(创建新配置文件的操作由对象执行,而不是视图。所以它不取决于您是否使用 sfGuardPlugin own管理员,或者如果您正在使用您的)。

    然后在sf_guard_user_profile类(sf_guard_user_profile.php)中,在save方法中可以这样做:

    public function save(PropelCon $con = null)
    {
      if ($this->isNew())
      {
        $this->setUserType(4);
      }
      parent::save();
    }
    

    这会将 UserType 设置为 4,用于任何新的配置文件。

    【讨论】:

    • 也许我做错了,但我所做的是创建了一个具有相同 sfGuardUser 架构的新前端“用户”模块,因此我可以拥有通常的索引、显示、编辑和新模板及其各自的操作。我需要这样做以精确控制某些用户在前端创建这些用户的方式。显然,缺点是在创建用户时不会自动创建配置文件。因此,虽然优雅,但您的解决方案对我不起作用。你能建议另一种解决方法吗?
    • @patrik 检查编辑,我从注册页面创建我的用户,没有管理员视图,只能通过调用 $user->save();并立即创建配置文件,与新创建的用户相关联。并且覆盖保存功能将允许您仅为每个新配置文件设置用户类型。
    • 我支持您提供的覆盖 save() 功能;我刚刚挂断了您在编辑中提到的 $user->save() 。现在设置的方式是在操作类中处理表单......请参阅我在问题中的更新。一旦用户被保存在插件提供的注册过程之外,它肯定不会创建用户配置文件。此外,我不希望每个用户都被指定为“4”类型,只是那些使用我创建的前端应用程序创建的用户。这有任何意义吗?所以我不确定你所说的“检查编辑”是什么意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多