我遇到了一个我似乎无法修复的问题,我正在尝试使用集合编辑某些双向的许多关系。
似乎在提交时我的原型条目都没有保存,只会考虑对初始关系所做的更改。
以下是我的一些代码:
public function buildForm(FormBuilderInterface $builder, array $options) {
$item = $builder->getData();
$builder
->add('serviceCol', CollectionType::class, array(
'entry_type' => ServiceType::class,
'entry_options' => array('label' => false,),
'property_path' => 'service',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'delete_empty' => true,
'prototype' => true,
'mapped' => false,
'data' => $item->getService()))
;
}
子表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($builder) {
$form = $event->getForm();
$service = $event->getData();
$form->add('service', EntityType::class, array(
'class' => Service::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('s')
->orderBy('s.id_service', 'ASC');
},
'choice_label' => 'nom',
'property_path' => 'nom',
'mapped' => false,
'label' => false,
'data' => $service,
'placeholder' => 'Aucun'));
});
}
entity - mappedBy:
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Service", mappedBy="applis")
*/
private $services;
public function __construct() {
$this->services = new ArrayCollection();
}
public function addService( Service $service = null ): bool {
if ($this->services->contains($service)) {
return false;
}
$this->services[] = $service;
return true;
}
public function remService( Service $service): bool {
return $this->services->removeElement($service);
}
public function getService() {
return $this->services;
}
实体 - 拥有:
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Appli", inversedBy="services" )
* @ORM\JoinTable(name="service_to_app",
* joinColumns={@ORM\JoinColumn(name="services", referencedColumnName="id_service")},
* inverseJoinColumns={@ORM\JoinColumn(name="applis", referencedColumnName="id_apps")}
* )
*/
private $applis;
public function __construct() {
$this->applis = new ArrayCollection();
}
public function addApp( Appli $app): bool {
//$app->addService($this);
if ($this->applis->contains($app)) {
return false;
}
$this->applis[] = $app;
return true;
}
public function remApp( Appli $app){
if ($app->remService($this)){
return $this->applis->removeElement($app);
}
return false;
}
public function getApp(){
return $this->applis;
}
dump($form->all())
甚至没有展示我的原型......
在POST_SUBMIT事件或任何事情中,我可以做些什么来在它们消失之前检索这些信息?
编辑:Javascript:
$(document).ready(function() {
// On récupère la balise <div> en question qui contient l'attribut « data-prototype » qui nous intéresse.
var $container = $('div#appli_form_serviceCol');
// On définit un compteur unique pour nommer les champs qu'on va ajouter dynamiquement
var index = $container.find(':input').length;
var $button = $('#add_field');
$button.click(function(e) {
addField($container);
//test();
e.preventDefault();
return false;
});
if (index == 0) {
addService($container);
} else {
$container.children('div').each(function() {
addDeleteLink($(this));
});
}
function addField($container) {
var template = $container.attr('data-prototype')
.replace(/__name__label__/g, 'service n°' + (index+1))
.replace(/__name__/g, index)
;
var $prototype = $(template);
addDeleteLink($prototype);
$container.append($prototype);
index++;
console.debug( $prototype);
}
function addDeleteLink($prototype) {
var $deleteLink = $('<a href="#" class="btn-danger"><button class="delButton">Supprimer</button></a>');
$prototype.append($deleteLink);
$deleteLink.click(function(e) {
$prototype.remove();
e.preventDefault();
return false;
});
}
});
让我知道如果有任何相关的代码部分我忘了。