schema.xml中
//This is my schema.
<?xml version="1.0" encoding="UTF-8"?>
<database name="default" namespace="UserBundle\Model" defaultIdMethod="native">
<table name="userEmail" phpName="userEmail" idMethod="native">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="user_id" phpName="UserId" type="INTEGER" required="false"/>
<column name="email_id" phpName="EmailId" type="VARCHAR" size="100" required="true"/>
<foreign-key foreignTable="users" name="user_email_ibfk_1" onDelete="RESTRICT" onUpdate="RESTRICT"><reference local="user_id" foreign="id"/></foreign-key>
<index name="userDetails_FI_1">
<index-column name="user_id"/>
</index>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="userMobile" phpName="userMobile" idMethod="native">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="user_id" phpName="UserId" type="INTEGER" required="false"/>
<column name="mobile_no" phpName="MobileNo" type="VARCHAR" size="100" required="true"/>
<foreign-key foreignTable="users" name="user_mobile_ibfk_1" onDelete="RESTRICT" onUpdate="RESTRICT"><reference local="user_id" foreign="id"/></foreign-key>
<index name="userDetails_FI_1">
<index-column name="user_id"/>
</index>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="users" phpName="Users" idMethod="native">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="first_name" phpName="FirstName" type="VARCHAR" size="100" required="true"/>
<column name="last_name" phpName="LastName" type="VARCHAR" size="100" required="true"/>
<column name="school_name" phpName="SchoolName" type="VARCHAR" size="100" required="true"/>
<column name="college_name" phpName="CollegeName" type="VARCHAR" size="100" required="true"/>
<column name="blood_group" phpName="BloodGroup" type="VARCHAR" size="10" required="true"/>
<column name="gender" phpName="Gender" type="VARCHAR" size="15" required="true"/>
<column name="date_of_birth" phpName="DateOfBirth" type="DATE" required="true"/>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
</database>
UserType.php
//This is my Form Builder page.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName');
$builder->add('lastName');
$builder->add('schoolName');
$builder->add('collegeName');
$builder->add('bloodGroup');
$builder->add('gender', 'choice', array(
'choices' => array(
'male' => 'Male',
'female' => 'Female'
),
'multiple' => false,
'expanded' => true,
'required' => true,
'data' => 'male'
));
$builder->add('dateOfBirth', 'date', array(
'years' => range(date('Y') - 80, date('Y') - 0)
));
$builder->add('userEmails', 'collection', array('type' => new userEmailType(),
'prototype' => true,
'data_class' => NULL,
'allow_add' =>true,
'allow_delete'=>true));
$builder->add('userMobiles', 'collection', array('type' => new userMobileType(),
'allow_add' =>true,
'allow_delete'=>true));
}
new.html.twig
//This is my twig page
<form action="" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit">
</form>
UserController.php
This is my controller page.
public function newAction() {
$users = new Users();
$userEmail = new userEmail();
$userMobile = new userMobile();
$form = $this->createForm(new UsersType(), $users);
$request = $this->getRequest();
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
$users->save();
return $this->redirect($this->generateUrl('user_listpage',array('pageId' =>1)));
}
}
return $this->render('UserBundle:Default:new.html.twig', array(
'form' => $form->createView(),
));
}
我使用上面的代码使用propel和symfony2将user_id作为外键插入用户详细信息,用户电子邮件和手机号。
如果我尝试插入其唯一的插入电子邮件ID和移动电话号码,而不是在子表中插入user_id(外键)值。
这是我在推进的symfony2中的第一个任务。