Symfony1 如何在处理表单(save())期间在单独的表中创建一对一的相关记录?

Symfony1 如何在处理表单(save())期间在单独的表中创建一对一的相关记录?,symfony1,doctrine,symfony-1.4,dql,Symfony1,Doctrine,Symfony 1.4,Dql,以下是模式: sf_guard_user columns: id { type: integer, primary: true, notnull: true, autoincrement: true, unique: true } username { type: string } firstname { type: string } lastname { type: s

以下是模式:

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
以下是我在前端尝试做的:我尝试允许前端用户创建新用户的。。。我能做到,一切顺利

在创建sf_guard_用户的save()过程中,创建新用户的sf_guard_用户配置文件,并将“user_id”列值设置为新创建的sf_guard_用户的主键(列“id”)。(2) 然后还将“用户类型”列设置为4

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

更新:

这是我的actions.class.php文件(项目/源文件/应用程序/模块/用户/操作):


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

all:
  sf_guard_plugin:
    profile_class:      sf_guard_user_profile
    profile_field_name: user_id
这将配置插件,以便在创建新用户时创建新配置文件(创建新配置文件的操作由对象执行,而不是由视图执行。因此,这并不取决于您是否使用sfGuardPlugin自己的管理员,或者您是否使用自己的管理员)

然后在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。

看起来您没有正确理解。正确配置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,即使您不使用它的管理生成器,它也会创建配置文件

也许我做得不对,但我所做的是用相同的sfGuardUser模式创建了一个新的前端“用户”模块,这样我就可以拥有通常的索引、显示、编辑和新模板以及它们各自的操作。我需要这样做,以便精确控制某些用户如何在前端创建这些用户。显然,缺点是在创建用户时不会自动创建配置文件。所以,虽然你的解决方案很优雅,但对我来说不起作用。你能建议另一种解决方法吗?@patrik检查编辑,我从注册页面创建我的用户,没有管理员视图,只需调用$user->save();并立即创建与新创建的用户关联的配置文件。对save函数的重写将允许您仅为每个新配置文件设置用户类型;我刚刚挂断了你在编辑中提到的$user->save()。现在的设置方式是表单在actions类中处理。。。请参阅问题中的我的更新。一旦用户被保存在pluggin提供的注册过程之外,它肯定不会创建用户配置文件。此外,我不希望每个用户都被指定为“4”类型,只是那些使用我创建的前端应用程序创建的用户。这有什么意义吗?所以我不确定你所说的“检查编辑”是什么意思。不过,这只是一个惯例:在设置类型之前,配置文件不会自动创建。。。
$user = $form->getObject();
$profile = $user->getProfile();
$profile->setUserType(4);
$profile->save();