Symfony 1.4 and editing/creating multiple models on one form?
I have a module, called Users, that allows me create, well, use开发者_运维百科rs. However, i also have a model called Profiles. It's a different table than Users, but whenever i create a new user, i want to add a new Profile. Also, i want to add in the Profile table two fields, available in the User form. Do you guys have any idea of how to do that in Symfony?
first of all you have to create a custom form inside your forms folder. Inside this form add all the fields you need to create your user. Then you have to change your processForm method (or you can do it inside the method that shows the form as Flask suggests)
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');
}
}
have a look at the sfdoctrineapply they are doing nearly exactly what you want to.
or in detail
#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
and in your form where you create the user:
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);
}
You must use embedded forms.
See this documentations:
http://symfony.com/blog/call-the-expert-customizing-sfdoctrineguardplugin
http://www.prettyscripts.com/framework/symfony/symfony-sfdoctrineguardplugin-and-customizing-user-profile
精彩评论