Php Symfony 1.4和在一个表单上编辑/创建多个模型?
我有一个叫做用户的模块,它允许我创建用户。但是,我还有一个名为Profiles的模型。这是一个与用户不同的表,但每当我创建一个新用户时,我都想添加一个新的配置文件。另外,我想在Profile表中添加两个字段,它们在用户表单中可用。你们知道如何在Symfony中做到这一点吗?看看他们正在做的几乎完全是你们想做的事情 或者详细地Php Symfony 1.4和在一个表单上编辑/创建多个模型?,php,symfony1,Php,Symfony1,我有一个叫做用户的模块,它允许我创建用户。但是,我还有一个名为Profiles的模型。这是一个与用户不同的表,但每当我创建一个新用户时,我都想添加一个新的配置文件。另外,我想在Profile表中添加两个字段,它们在用户表单中可用。你们知道如何在Symfony中做到这一点吗?看看他们正在做的几乎完全是你们想做的事情 或者详细地 #schema for the profile sfGuardUserProfile: tableName: sf_guard_user_profile colu
#schema for the profile
sfGuardUserProfile:
tableName: sf_guard_user_profile
columns:
id:
type: integer(4)
primary: true
autoincrement: true
user_id:
type: integer(4)
notnull: true
email:
type: string(80)
fullname:
type: string(80)
validate:
type: string(17)
# Don't forget this!
relations:
User:
class: sfGuardUser
foreign: id
local: user_id
type: one
onDelete: cascade
foreignType: one
foreignAlias: Profile
在创建用户的表单中:
public function doSave($con = null)
{
$user = new sfGuardUser();
$user->setUsername($this->getValue('username'));
$user->setPassword($this->getValue('password'));
// They must confirm their account first
$user->setIsActive(false);
$user->save();
$this->userId = $user->getId();
return parent::doSave($con);
}
首先,您必须在表单文件夹中创建自定义表单。在此表单中添加创建用户所需的所有字段。然后您必须更改processForm方法,或者您可以在如Flask所建议的显示表单的方法中进行更改
protected function processForm(sfWebRequest $request, sfForm $form){
$form->bind($request->getParameter('registration'));
if ($form->isValid())
{
$user= new sfGuardUser();
$user->setUsername($form->getValue('username'));
$user->setPassword($form->getValue('password'));
$user->setIsActive(true);
$user->save();
$profile= new sfGuardUserProfile();
$profile->setUserId($user->getId());
$profile->setName($form->getValue('nombre'));
$profile->setSurname($form->getValue('apellidos'));
$profile->setMail($form->getValue('username'));
$profile->save();
$this->redirect('@user_home');
}
}您必须使用嵌入式表单。 请参阅以下文档: