Symfony2动态表单修改不保存生成的数据

时间:2016-03-03 17:52:33

标签: symfony symfony-2.7

我疯了,因为如果我从实体字段中选择一个客户端,它会正确填充名为proposal的第二个实体字段。然后我选择动态生成的提案,但是当我保存表单时,它会正确保存表单但不填写提案字段。我按照Symfony教程了解动态表格,可以找到here

这是我的FormType代码:

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;
}

}

我哪里错了?

1 个答案:

答案 0 :(得分:0)

您确定提案查询是否正确?

$proposals = null === $client ? array() : $this->em->getRepository('AppBundle:Proposals')->findBy(
    array('client'=>$client->getId()),
    array('id' => 'DESC'));

这不应该是array('client_id' => $client->getId()),array('client' => $client),吗?

尝试通过在下方添加dump($proposals)并在symfony探查器中查找结果来检查$ proposal的实际内容。

相关问题