Easyadmin CollectionType字段未保存oneToMany关系

时间:2018-02-20 11:10:44

标签: php doctrine one-to-many symfony-3.4 easyadmin

我有一个字段集合类型,但是当我保存它时,它不会保存关系oneToMany。

我的代码是:

Config.yml:

Splitter:
        class: AppBundle\Entity\Splitter
        controller: AppBundle\Controller\EasyAdmin\SplitterController
        list:
            fields:
                - caixa.elemento.nome
                - paridadeVias
        form:
            fields:
                - caixa
                - paridadeVias
                - balanceamentoCaixa
                - dataHoraInstalacao
                - { property: 'splitterSaida', type: 'collection', type_options: { entry_type: 'AppBundle\Form\SplitterSaidaForm', by_reference: false } }

看我有一个特定的控制器,我的属性是一个集合类型

SplitterController:

protected function newAction()
{
    $this->dispatch(EasyAdminEvents::PRE_NEW);

    $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');

    $easyadmin = $this->request->attributes->get('easyadmin');
    $easyadmin['item'] = $entity;
    $this->request->attributes->set('easyadmin', $easyadmin);

    $fields = $this->entity['new']['fields'];

    $newForm = $this->createForm(SplitterForm::class, $entity);

    $newForm->handleRequest($this->request);
    if ($newForm->isSubmitted() && $newForm->isValid()) {
        $this->dispatch(EasyAdminEvents::PRE_PERSIST, array('entity' => $entity));

        $this->executeDynamicMethod('prePersist<EntityName>Entity', array($entity));
        $this->executeDynamicMethod('persist<EntityName>Entity', array($entity));

        $this->dispatch(EasyAdminEvents::POST_PERSIST, array('entity' => $entity));

        return $this->redirectToReferrer();
    }

    $this->dispatch(EasyAdminEvents::POST_NEW, array(
        'entity_fields' => $fields,
        'form' => $newForm,
        'entity' => $entity,
    ));

    return $this->render('easy_admin/Splitter/new.html.twig', array(
        'form' => $newForm->createView(),
        'entity_fields' => $fields,
        'entity' => $entity,
    ));
}


protected function editAction()
{
    $this->dispatch(EasyAdminEvents::PRE_EDIT);

    $id = $this->request->query->get('id');
    $easyadmin = $this->request->attributes->get('easyadmin');
    $entity = $easyadmin['item'];

    if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
        $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
        $fieldsMetadata = $this->entity['list']['fields'];

        if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
            throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
        }

        $this->updateEntityProperty($entity, $property, $newValue);

        // cast to integer instead of string to avoid sending empty responses for 'false'
        return new Response((int)$newValue);
    }

    $fields = $this->entity['edit']['fields'];

    $editForm = $this->createForm(SplitterForm::class, $entity);
    $deleteForm = $this->createDeleteForm($this->entity['name'], $id);

    $editForm->handleRequest($this->request);
    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->dispatch(EasyAdminEvents::PRE_UPDATE, array('entity' => $entity));

        $this->executeDynamicMethod('preUpdate<EntityName>Entity', array($entity));
        $this->executeDynamicMethod('update<EntityName>Entity', array($entity));


        $this->dispatch(EasyAdminEvents::POST_UPDATE, array('entity' => $entity));

        return $this->redirectToReferrer();
    }

    $this->dispatch(EasyAdminEvents::POST_EDIT);

    return $this->render('easy_admin/Splitter/new.html.twig', array(
        'form' => $editForm->createView(),
        'entity_fields' => $fields,
        'entity' => $entity,
        'delete_form' => $deleteForm->createView(),
    ));
}

SplitterEntity:

/**
 * @ORM\OneToMany(
 *     targetEntity="SplitterSaida",
 *     mappedBy="splitter",
 *     fetch="EAGER",
 *     orphanRemoval=true,
 *     cascade={"persist"}
 * )
 * @Assert\Valid()
 */
private $splitterSaida;

 public function addSplitterSaida(SplitterSaida $splitterSaida)
{
    $this->splitterSaida->add($splitterSaida);
    $splitterSaida->setSplitter($this);

    return $this;
}

public function removeSplitterSaida(SplitterSaida $splitterSaida)
{
    if (!$this->splitterSaida->contains($splitterSaida)) {
        return;
    }

    $this->splitterSaida->removeElement($splitterSaida);
    $splitterSaida->setSplitter(null);
}

/**
 * @return ArrayCollection|SplitterSaida[]
 */
public function getSplitterSaida()
{
    return $this->splitterSaida;
}

/**
 * @param mixed $splitterSaida
 */
public function setSplitterSaida($splitterSaida)
{
    $this->splitterSaida = $splitterSaida;
}

永远不会调用addSplitterSaida方法

SplitterSaidaEntity:

/**
 * @ORM\ManyToOne(targetEntity="Splitter", inversedBy="splitterSaida")
 */
private $splitter;

总结......

为什么我的关系没有保存? 为什么从未调用过方法addSplitterSaida?

请帮助我!谢谢!

0 个答案:

没有答案