2011-08-22 91 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'); 
} 

}

相關問題