可捕获的致命错误:传递给方法的参数无效,预计会有一些时间和null给定

时间:2017-05-15 15:13:40

标签: php symfony symfony-forms

我执行项目时遇到此问题,我在FormEvent

中使用FormType

enter image description here

entity Departement.php:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Departement
 *
 * @ORM\Table(name="departement")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DepartementRepository")
 */
class Departement
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var int
     *
     * @ORM\Column(name="code", type="integer")
     */
    private $code;


    /**
     *@ORM\ManyToOne(targetEntity="AppBundle\Entity\Region")
    */
    private $region;

    /**
     *@ORM\OneToMany(targetEntity="AppBundle\Entity\Ville", mappedBy="departement")
    */
    private $villes;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Departement
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set code
     *
     * @param integer $code
     * @return Departement
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return integer 
     */
    public function getCode()
    {
        return $this->code;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->villes = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set region
     *
     * @param \AppBundle\Entity\Region $region
     * @return Departement
     */
    public function setRegion(\AppBundle\Entity\Region $region = null)
    {
        $this->region = $region;

        return $this;
    }

    /**
     * Get region
     *
     * @return \AppBundle\Entity\Region 
     */
    public function getRegion()
    {
        return $this->region;
    }

    /**
     * Add villes
     *
     * @param \AppBundle\Entity\Ville $villes
     * @return Departement
     */
    public function addVille(\AppBundle\Entity\Ville $villes)
    {
        $this->villes[] = $villes;

        return $this;
    }

    /**
     * Remove villes
     *
     * @param \AppBundle\Entity\Ville $villes
     */
    public function removeVille(\AppBundle\Entity\Ville $villes)
    {
        $this->villes->removeElement($villes);
    }

    /**
     * Get villes
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getVilles()
    {
        return $this->villes;
    }

    public function __toString(){
        return $this->name;
    }
}

entity ville.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Ville
 *
 * @ORM\Table(name="ville")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\VilleRepository")
 */
class Ville
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var int
     *
     * @ORM\Column(name="code", type="integer")
     */
    private $code;


    /**
     *@ORM\ManyToOne(targetEntity="AppBundle\Entity\Departement")
    */
    private $departement;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Ville
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set code
     *
     * @param integer $code
     * @return Ville
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return integer 
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * Set departement
     *
     * @param \AppBundle\Entity\Departement $departement
     * @return Ville
     */
    public function setDepartement(\AppBundle\Entity\Departement $departement = null)
    {
        $this->departement = $departement;

        return $this;
    }

    /**
     * Get departement
     *
     * @return \AppBundle\Entity\Departement 
     */
    public function getDepartement()
    {
        return $this->departement;
    }

    public function __toString(){
        return $this->name;
    }
}

表格 MedecinType.php

<?php

  namespace AppBundle\Form;

  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormInterface;
  use AppBundle\Entity\Region;
  use AppBundle\Entity\Departement;
  use AppBundle\Entity\Ville;

  class MedecinType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
   $builder
    ->add('name')
    ->add('region', EntityType::class, [
      'class'       => 'AppBundle\Entity\Region',
      'placeholder' => 'Sélectionnez votre région',
      'mapped'      => false,
      'required'    => false
    ]);


            $builder->get('region')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) {

             $form = $event->getForm();
             $this->addDepartementField($form->getParent(), $form->getData());
   }
  );

      }


      private function addDepartementField(FormInterface $form, Region $region)
  {
     $builder = $form->getConfig()->getFormFactory()->createNamedBuilder(
                'departement',
                EntityType::class,
                null,
                [
                 'class' => 'AppBundle\Entity\Departement',
                 'placeholder' => 'Sélectionnez votre département',
                 'mapped' => false,
                 'required' => false,
                 'auto_initialize' => false,
                 'choices' => $region->getDepartements()
              ]);

            $builder->addEventListener(
                FormEvents::POST_SUBMIT,
                function(FormEvent $event) {
                  $form= $event->getForm();
                  $this->addVilleField($form->getParent(), $form->getData());

                }

              );

            $form->add($builder->getForm());

  }

  private function addVilleField(FormInterface $form, Departement $departement)
  {
    $form->add('ville', EntityType::class, [
      'class'       => 'AppBundle\Entity\Ville',
      'placeholder' => 'Sélectionnez votre ville',
      'choices'     => $departement->getVilles()
    ]);
  }

      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver->setDefaults(array(
              'data_class' => 'AppBundle\Entity\Medecin'
          ));
      }

      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'appbundle_medecin';
      }


  }

请帮我解决这个问题,谢谢高级

1 个答案:

答案 0 :(得分:1)

由于您已将departement字段设置为'required' => false,可为空,因此事件侦听器中的方法$form->getData()可以是Departement实体的实例,也可以是{{1}如果没有选择null

您必须检查departement是否返回您实体的实例 处理,如果不是。

这就像是:

$form->getData()