Symfony2动态表单修改不保存生成的数据
我疯了,因为如果我从一个实体字段中选择一个客户机,它会正确地填充第二个实体字段,称为Propositions。然后我选择动态生成的提案,但当我保存表单时,它会正确保存表单,但不会填充提案字段。我遵循了Symfony教程中关于动态表单的内容,可以找到动态表单 这是我的表单类型代码:Symfony2动态表单修改不保存生成的数据,symfony,symfony-2.7,Symfony,Symfony 2.7,我疯了,因为如果我从一个实体字段中选择一个客户机,它会正确地填充第二个实体字段,称为Propositions。然后我选择动态生成的提案,但当我保存表单时,它会正确保存表单,但不会填充提案字段。我遵循了Symfony教程中关于动态表单的内容,可以找到动态表单 这是我的表单类型代码: public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('client',
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('client', 'entity', array(
'class' => 'AppBundle\Entity\Client',
'property' => 'name',
'label' => 'Client:',
'empty_value' => '',
'required' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
));
$formModifier = function (FormInterface $form, Client $client = null) {
$proposals = null === $client ? array() : $this->em->getRepository('AppBundle:Proposals')->findBy(
array('client'=>$client->getId()),
array('id' => 'DESC'));
$form->add('proposal', 'entity', array(
'class' => 'AppBundle\Entity\Proposal',
'choice_label' => 'subject',
'placeholder' => '',
'choices' => $proposals,
'label' => 'Proposal',
'required' => false
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$client = null;
$data = $event->getData();
if(!empty($data)) {
$client = $data->getClient();
}
$formModifier($event->getForm(), $client );
}
);
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$client = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $client);
}
);
这是Prenotazione实体,属于形体的实体
class Prenotazione {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Client", inversedBy="prenotazioni")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* @ORM\OneToOne(targetEntity="Proposal", inversedBy="prenotazione")
* @ORM\JoinColumn(name="proposal_id", referencedColumnName="id")
*/
private $proposal;
public function getId() {
return $this->id;
}
public function setProposal(\AppBundle\Entity\Proposal $proposal = null)
{
$this->proposal = $proposal;
return $this;
}
public function getProposal() {
return $this->proposal;
}
public function setClient(\AppBundle\Entity\Client $client = null)
{
$this->client = $client;
return $this;
}
public function getClient()
{
return $this->client;
}
}
我哪里错了?你确定你的建议是正确的吗
$proposals = null === $client ? array() : $this->em->getRepository('AppBundle:Proposals')->findBy(
array('client'=>$client->getId()),
array('id' => 'DESC'));
这不是应该是array('client\u id'=>$client->getId()),
还是array('client'=>$client'),
通过在下方添加一个
转储($propositions)
并在symfony profiler中查找结果,尝试检查$propositions的实际内容。您确定您的建议查询是正确的吗
$proposals = null === $client ? array() : $this->em->getRepository('AppBundle:Proposals')->findBy(
array('client'=>$client->getId()),
array('id' => 'DESC'));
这不是应该是array('client\u id'=>$client->getId()),
还是array('client'=>$client'),
尝试通过在下方添加一个
转储($proposities)
并在symfony profiler中查找结果来检查$proposities的实际内容。您可以向我们展示您的实体吗?问题中添加了我second@PawełMikołajczuk。谢谢。你能给我们看看你的实体吗?我在问题中添加了第二个@PawełMikołajczuk。谢谢,你是说var_dump?从未听说过dump函数,当我尝试它时,它会给我一个错误。即使使用“client”=>$client,代码也很好。返回的结果很好。唯一的大问题是它不能保存我的选择你是说var_dump?从未听说过dump函数,当我尝试它时,它会给我一个错误。即使使用“client”=>$client,代码也很好。返回的结果很好。唯一的大问题是这并不能挽救我的选择