简体   繁体   English

在原则中处理一对一的单向关系

[英]Working with One-to-One unidirectional relationship in Doctrine

I have an entity named Install that has two properties: a hostname and a service call number. 我有一个名为Install的实体,它具有两个属性:主机名和服务呼叫号码。 The service call number is optional. 服务电话号码是可选的。 Rather than allow the field to be null and violate 1NF, I created a second entity called ServiceCall that has a one-to-one unidirectional relationship to the Install entity. 我创建了另一个名为ServiceCall的实体,该实体与Install实体具有一对一的单向关系,而不是允许该字段为null并违反1NF。 My problem is that when I enter both a hostname and a service call into the form and submit, only the hostname is persisted, not the service call and the relationship. 我的问题是,当我在表单中输入主机名和服务调用并提交时,仅主机名被保留,服务调用和关系不被保留。 Here is my code: 这是我的代码:

Service Call Entity 服务呼叫实体

class ServiceCall
{
/**
 * @var integer
 */
private $id;

/**
 * @var integer
 */
private $serviceCall;

private $install;

// Getters / Setters
}

Install Entity 安装实体

class Install
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $hostname;

private $serviceCall;

// Getters / Setters
}

Relationship config: 关系配置:

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

Controller method: 控制器方式:

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(),
    ));
}

Install Form type 安装表格类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('hostname')
        ->add('serviceCall', new ServiceCallType())
    ;
}

ServiceCall Form Type 服务电话表格类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('serviceCall')
    ;
}

You must add OneToOne relation for Install entry, not for ServiceCall entity. 您必须为Install条目而不是ServiceCall实体添加OneToOne关系。

And not forgot add cascade persist options for relation. 并且不要忘记为关系添加级联持久选项。

And also need create new ServiceCall entity for relation. 并且还需要创建新的ServiceCall实体进行关联。

For this use DataTransformer in ServiceCallFormType 为此,请使用ServiceCallFormType中的DataTransformer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM