У меня есть тип коллекции полей, но когда я сохраняю его, не сохраняются отношения 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 никогда не вызывался?
Пожалуйста, помогите мне! Спасибо!
Задача ещё не решена.
Других решений пока нет …