2011-08-22 66 views
0

我有一个叫做Users的模块,它允许我创建用户。但是,我也有一个名为Profiles的模型。它与用户不同,但每当我创建一个新用户时,我想添加一个新的配置文件。另外,我想在配置文件表中添加两个字段,在用户窗体中可用。你们有没有想过在Symfony中如何做到这一点?Symfony 1.4和在一个表单上编辑/创建多个模型?

回答

0

看看sfdoctrineapply他们几乎完全符合你的要求。

或详细

#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); 
    } 
0

首先你必须创建表单文件夹中的自定义表单。在此表单中添加创建用户所需的所有字段。然后,你必须改变你的processForm方法(或者你可以做到这一点,显示形式瓶暗示方法内)

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

}

相关问题