我有一个名为Install的实体,它有两个属性:主机名和服务呼叫号。服务电话号码是可选的。我没有允许该字段为空并违反1NF,而是创建了第二个名为ServiceCall的实体,该实体与Install实体具有一对一的单向关系。我的问题是,当我在表单中输入主机名和服务调用并提交时,只保留主机名,而不是服务调用和关系。这是我的代码:
服务电话实体
class ServiceCall
{
/**
* @var integer
*/
private $id;
/**
* @var integer
*/
private $serviceCall;
private $install;
// Getters / Setters
}
安装实体
class Install
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $hostname;
private $serviceCall;
// Getters / Setters
}
关系配置:
AppBundle\Entity\Install:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
hostname:
type: string
length: 255
lifecycleCallbacks: { }
AppBundle\Entity\ServiceCall:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
serviceCall:
type: integer
lifecycleCallbacks: { }
oneToOne:
install:
targetEntity: AppBundle\Entity\Install
joinColumn:
name: install_id
referencedColumnName: id
控制器方法:
public function createAction(Request $request)
{
$entity = new Install();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('install_show', array('id' => $entity->getId())));
}
return $this->render('AppBundle:Install:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
安装表单类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('hostname')
->add('serviceCall', new ServiceCallType())
;
}
ServiceCall表单类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('serviceCall')
;
}
答案 0 :(得分:1)
您必须为Install条目添加OneToOne关系,而不是为ServiceCall实体添加。
并且不要忘记为关系添加级联持久性选项。
还需要为关系创建新的ServiceCall实体。
为此,请使用ServiceCallFormType中的DataTransformer