title: Argument 1 passed to Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() must be an object or null, int given
body: <p>I'm new with Symfony. I've followed a <a href="https://symfonycasts.com/screencast/symfony-forms/choice-type-fields" rel="nofollow noreferrer">couple</a> of <a href="https://ourcodeworld.com/articles/read/652/how-to-create-a-dependent-select-dependent-dropdown-in-symfony-3" rel="nofollow noreferrer">tutorials</a> trying to learn dependent selects, so I use a DestinationFormType and a DestinationController to save a Destination. The JS part works well, but now when I try to save the entity throws the next exception:</p>
  <blockquote>
  <p>Argument 1 passed to Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() must be an object or null, int given, called in \vendor\symfony\form\ChoiceList\ArrayChoiceList.php on line 200</p>
  </blockquote>
  <p>The weird part is that it seems to be trying to convert a value (191) that need no conversion (stack call top):
  <a href="https://i.stack.imgur.com/d7G0L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d7G0L.png" alt="enter image description here" /></a></p>
  <p>The error is thrown for the country entity. This is my DestinationFormType</p>
  <pre><code>&lt;?php
  
  
  namespace App\Form;
  
  
  use App\Entity\City;
  use App\Entity\Country;
  use App\Entity\Destination;
  use App\Entity\DestinationCategory;
  use App\Entity\DestinationSubcategory;
  use App\Entity\Region;
  use App\Entity\State;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\Form\FormInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class DestinationFormType extends AbstractType
  {
      /** @var EntityManagerInterface */
      private $em;
  
      /**
       * @param EntityManagerInterface $em
       */
      public function __construct(EntityManagerInterface $em)
      {
          $this-&gt;em = $em;
      }
  
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
              -&gt;add('coordinates')
              -&gt;add('kidsCost')
              -&gt;add('adultsCost')
              -&gt;add('description')
          ;
  
          $builder-&gt;addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
          $builder-&gt;addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; Destination::class
          ]);
      }
  
      /**
       * @param FormEvent $event
       */
      public function onPreSubmit(FormEvent $event) {
          $form = $event-&gt;getForm();
          $data = $event-&gt;getData();
  
          /** @var Region $region */
          $region = $this-&gt;em
              -&gt;getRepository(Region::class)
              -&gt;find($data['region']);
  
          /** @var Country $country */
          $country = $this-&gt;em
              -&gt;getRepository(Country::class)
              -&gt;find($data['country']);
  
          /** @var State $state */
          $state = $this-&gt;em
              -&gt;getRepository(State::class)
              -&gt;find($data['state']);
  
          /** @var City $city */
          $city = $this-&gt;em
              -&gt;getRepository(City::class)
              -&gt;find($data['city']);
  
          /** @var DestinationCategory $city */
          $category = $this-&gt;em
              -&gt;getRepository(DestinationCategory::class)
              -&gt;find($data['category']);
  
          /** @var DestinationCategory $city */
          $subcategory = $this-&gt;em
              -&gt;getRepository(DestinationSubcategory::class)
              -&gt;find($data['subcategory']);
  
          $this-&gt;addLocationDropdowns($form, $region, $country, $state, $city);
          $this-&gt;addCategoryDropdowns($form, $category, $subcategory);
      }
  
      /**
       * @param FormEvent $event
       */
      public function onPreSetData(FormEvent $event) {
          /** @var Destination $destination */
          $destination = $event-&gt;getData();
  
          $form = $event-&gt;getForm();
          $category =  $destination &amp;&amp; $destination-&gt;getCategory() ? $destination-&gt;getCategory() : null;
          $subcategory =  $destination &amp;&amp; $destination-&gt;getSubcategory() ? $destination-&gt;getSubcategory() : null;
  
          $region = $destination &amp;&amp; $destination-&gt;getRegion() ? $destination-&gt;getRegion() : null;
          $country = $destination &amp;&amp; $destination-&gt;getCountry() ? $destination-&gt;getCountry() : null;
          $state = $destination &amp;&amp; $destination-&gt;getState() ? $destination-&gt;getState() : null;
          $city = $destination &amp;&amp; $destination-&gt;getCity() ? $destination-&gt;getCity() : null;
  
          $this-&gt;addLocationDropdowns($form, $region, $country, $state, $city);
          $this-&gt;addCategoryDropdowns($form, $category, $subcategory);
      }
  
      /**
       * @param FormInterface $form
       * @param Region|null $region
       * @param Country|null $country
       * @param State|null $state
       * @param City|null $city
       */
      private function addLocationDropdowns(FormInterface $form, ?Region $region, ?Country $country, ?State $state, ?City $city) {
          $this-&gt;addRegionDropDown($form, $region);
          $this-&gt;addCountryDropdown($form, $region, $country);
          $this-&gt;addStateDropdown($form, $country, $state);
          $this-&gt;addCityDropdown($form, $state, $city);
      }
  
      /**
       * @param FormInterface $form
       * @param Region|null $region
       */
      private function addRegionDropDown(FormInterface $form, ?Region $region)
      {
          $form-&gt;add('region', EntityType::class,[
              'required' =&gt; true,
              'data' =&gt; $region,
              'placeholder' =&gt; 'Select a Region...',
              'class' =&gt; Region::class
          ]);
      }
  
      /**
       * @param FormInterface $form
       * @param Region|null $region
       * @param Country|null $country
       */
      private function addCountryDropdown(FormInterface $form, ?Region $region, ?Country $country)
      {
          $countries = array();
  
          if ($region) {
              $countryRepository = $this-&gt;em-&gt;getRepository(Country::class);
              $countries = $countryRepository-&gt;findByRegionId($region-&gt;getId());
          }
  
          $form-&gt;add('country', EntityType::class, [
              'required' =&gt; true,
              'data' =&gt; $country,
              'placeholder' =&gt; 'Select a Region first ...',
              'class' =&gt; Country::class,
              'choices' =&gt; $countries
          ]);
      }
  
      /**
       * @param FormInterface $form
       * @param Country|null $country
       * @param State|null $state
       */
      private function addStateDropdown(FormInterface $form, ?Country $country, ?State $state)
      {
          $states = array();
  
          if ($country) {
              $stateRepository = $this-&gt;em-&gt;getRepository(State::class);
              $states = $stateRepository-&gt;findByCountryId($country-&gt;getId());
          }
  
          $form-&gt;add('state', EntityType::class, [
              'required' =&gt; true,
              'data' =&gt; $state,
              'placeholder' =&gt; 'Select a Country first ...',
              'class' =&gt; State::class,
              'choices' =&gt; $states
          ]);
      }
  
      /**
       * @param FormInterface $form
       * @param State|null $state
       * @param City|null $city
       */
      private function addCityDropdown(FormInterface $form, ?State $state, ?City $city)
      {
          $cities = array();
  
          if ($state) {
              $cityRepository = $this-&gt;em-&gt;getRepository(City::class);
              $cities = $cityRepository-&gt;findByStateId($state-&gt;getId());
          }
  
          $form-&gt;add('city', EntityType::class, [
              'required' =&gt; true,
              'data' =&gt; $city,
              'placeholder' =&gt; 'Select a State first ...',
              'class' =&gt; State::class,
              'choices' =&gt; $cities
          ]);
      }
  
      /**
       * @param FormInterface $form
       * @param DestinationCategory|null $category
       * @param DestinationSubcategory|null $subcategory
       */
      private function addCategoryDropdowns(FormInterface $form, ?DestinationCategory $category, ?DestinationSubcategory $subcategory)
      {
          $this-&gt;addCategoryDropDown($form, $category);
          $this-&gt;addSubcategoryDropDown($form, $category, $subcategory);
      }
  
      /**
       * @param FormInterface $form
       * @param DestinationCategory|null $category
       */
      private function addCategoryDropDown(FormInterface $form, ?DestinationCategory $category)
      {
          $form-&gt;add('category', EntityType::class,[
              'required' =&gt; true,
              'data' =&gt; $category,
              'placeholder' =&gt; 'Select a Category...',
              'class' =&gt; DestinationCategory::class
          ]);
      }
  
      /**
       * @param FormInterface $form
       * @param DestinationCategory|null $category
       * @param DestinationSubcategory|null $subcategory
       */
      private function addSubcategoryDropDown(FormInterface $form, ?DestinationCategory $category, ?DestinationSubcategory $subcategory)
      {
          $subcategories = array();
  
          if ($category) {
              $countryRepository = $this-&gt;em-&gt;getRepository(DestinationSubcategory::class);
              $subcategories = $countryRepository-&gt;findByCategoryId($category-&gt;getId());
          }
  
          $form-&gt;add('subcategory', EntityType::class, [
              'required' =&gt; true,
              'data' =&gt; $subcategory,
              'placeholder' =&gt; 'Select a Category first ...',
              'class' =&gt; DestinationSubcategory::class,
              'choices' =&gt; $subcategories
          ]);
      }
  }
  </code></pre>
  <p>This is my DestinationController</p>
  <pre><code>&lt;?php
  
  
  namespace App\Controller;
  
  
  use App\Entity\Destination;
  use App\Form\DestinationFormType;
  use App\Repository\DestinationRepository;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Routing\Annotation\Route;
  
  class DestinationController extends AbstractController
  {
      /**
       * @Route(&quot;admin/destination/new&quot;, name=&quot;admin_destination_new&quot;)
       *
       * @param EntityManagerInterface $em
       * @param Request $request
       *
       * @return Response
       */
      public function new(EntityManagerInterface $em, Request $request)
      {
          $form = $this-&gt;createForm(DestinationFormType::class);
  
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              /** @var Destination $destination */
              $destination = $form-&gt;getData();
              $em-&gt;persist($destination);
              $em-&gt;flush();
  
              $this-&gt;addFlash('success', 'Destination created');
              $this-&gt;redirectToRoute('admin_destination_list');
          }
  
          return $this-&gt;render('destination/new.html.twig', [
              'destForm' =&gt; $form-&gt;createView()
          ]);
      }
  
      /**
       * @Route(&quot;admin/destination/{id}/edit&quot;, name=&quot;admin_destination_edit&quot;)
       *
       * @param Destination $destination
       * @param EntityManagerInterface $em
       * @param Request $request
       *
       * @return Response
       */
      public function edit(Destination $destination, EntityManagerInterface $em, Request $request)
      {
          $form = $this-&gt;createForm(DestinationFormType::class, $destination);
  
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $em-&gt;persist($destination);
              $em-&gt;flush();
  
              $this-&gt;addFlash('success', 'Destiny updated');
              $this-&gt;redirectToRoute('admin_destination_list');
          }
  
          return $this-&gt;render('destination/edit.html.twig', [
              'destForm' =&gt; $form-&gt;createView()
          ]);
      }
  
      /**
       * @Route(&quot;admin/destination&quot;, name=&quot;admin_destination_list&quot;)
       * @param DestinationRepository $destRepo
       *
       * @return Response
       */
      public function list(DestinationRepository $destRepo){
          /** @var Destination $destinations */
          $destinations = $destRepo-&gt;findAll();
  
          return $this-&gt;render('destination/list.html.twig', [
              'destinations' =&gt; $destinations
          ]);
      }
  }
  </code></pre>
  <p>And this is my Destination entity</p>
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  use Gedmo\Timestampable\Traits\TimestampableEntity;
  use Gedmo\Mapping\Annotation as Gedmo;
  
  /**
   * @ORM\Entity(repositoryClass=&quot;App\Repository\DestinationRepository&quot;)
   */
  class Destination
  {
      use TimestampableEntity;
      use SoftDeleteableEntity;
  
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;)
       */
      private $id;
  
      /**
       * @ORM\Column(type=&quot;string&quot;, length=150)
       */
      private $name;
  
      /**
       * @ORM\Column(type=&quot;string&quot;, length=150, nullable=true)
       */
      private $coordinates;
  
      /**
       * @ORM\Column(type=&quot;string&quot;, length=150, unique=true)
       * @Gedmo\Slug(fields={&quot;name&quot;})
       */
      private $slug;
  
      /**
       * @ORM\Column(type=&quot;decimal&quot;, precision=8, scale=2, nullable=true)
       */
      private $kidsCost;
  
      /**
       * @ORM\Column(type=&quot;decimal&quot;, precision=8, scale=2, nullable=true)
       */
      private $adultsCost;
  
      /**
       * @ORM\ManyToMany(targetEntity=&quot;App\Entity\Package&quot;, mappedBy=&quot;destinations&quot;)
       */
      private $packages;
  
      /**
       * @ORM\Column(type=&quot;text&quot;, nullable=true)
       */
      private $description;
  
      /**
       * @ORM\Column(type=&quot;string&quot;, length=255, nullable=true)
       */
      private $address;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\City&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $city;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\State&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $state;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Region&quot;, inversedBy=&quot;destinations&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $region;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Country&quot;, inversedBy=&quot;destinations&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $country;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\DestinationCategory&quot;, inversedBy=&quot;destinations&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $category;
  
      /**
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\DestinationSubcategory&quot;, inversedBy=&quot;destinations&quot;)
       * @ORM\JoinColumn(nullable=false)
       */
      private $subcategory;
  
  
      public function __construct()
      {
          $this-&gt;packages = new ArrayCollection();
      }
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getName(): ?string
      {
          return $this-&gt;name;
      }
  
      public function setName(string $name): self
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      public function getCoordinates(): ?string
      {
          return $this-&gt;coordinates;
      }
  
      public function setCoordinates(?string $coordinates): self
      {
          $this-&gt;coordinates = $coordinates;
  
          return $this;
      }
  
      public function getSlug(): ?string
      {
          return $this-&gt;slug;
      }
  
      public function setSlug(string $slug): self
      {
          $this-&gt;slug = $slug;
  
          return $this;
      }
  
      public function getKidsCost(): ?string
      {
          return $this-&gt;kidsCost;
      }
  
      public function setKidsCost(?string $kidsCost): self
      {
          $this-&gt;kidsCost = $kidsCost;
  
          return $this;
      }
  
      public function getAdultsCost(): ?string
      {
          return $this-&gt;adultsCost;
      }
  
      public function setAdultsCost(?string $adultsCost): self
      {
          $this-&gt;adultsCost = $adultsCost;
  
          return $this;
      }
  
      /**
       * @return Collection|Package[]
       */
      public function getPackages(): Collection
      {
          return $this-&gt;packages;
      }
  
      public function addPackage(Package $package): self
      {
          if (!$this-&gt;packages-&gt;contains($package)) {
              $this-&gt;packages[] = $package;
              $package-&gt;addDestination($this);
          }
  
          return $this;
      }
  
      public function removePackage(Package $package): self
      {
          if ($this-&gt;packages-&gt;contains($package)) {
              $this-&gt;packages-&gt;removeElement($package);
              $package-&gt;removeDestination($this);
          }
  
          return $this;
      }
  
      public function getDescription(): ?string
      {
          return $this-&gt;description;
      }
  
      public function setDescription(?string $description): self
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      public function getAddress(): ?string
      {
          return $this-&gt;address;
      }
  
      public function setAddress(?string $address): self
      {
          $this-&gt;address = $address;
  
          return $this;
      }
  
      public function getCity(): ?City
      {
          return $this-&gt;city;
      }
  
      public function getState(): ?State
      {
          return $this-&gt;getCity() ? $this-&gt;getCity()-&gt;getState() : null;
      }
  
      public function getCountry(): ?Country
      {
          return $this-&gt;getCity()? $this-&gt;getState()-&gt;getCountry() : null;
      }
  
      public function getRegion() : ?Region
      {
          return $this-&gt;getCity()? $this-&gt;getCountry()-&gt;getRegion() : null;
      }
  
      public function setCity(?City $city): self
      {
          $this-&gt;city = $city;
  
          return $this;
      }
  
      public function setState(?State $state): self
      {
          $this-&gt;state = $state;
  
          return $this;
      }
  
      public function setRegion(?Region $region): self
      {
          $this-&gt;region = $region;
  
          return $this;
      }
  
      public function setCountry(?Country $country): self
      {
          $this-&gt;country = $country;
  
          return $this;
      }
  
      public function getCategory(): ?DestinationCategory
      {
          return $this-&gt;category;
      }
  
      public function setCategory(?DestinationCategory $category): self
      {
          $this-&gt;category = $category;
  
          return $this;
      }
  
      public function getSubcategory(): ?DestinationSubcategory
      {
          return $this-&gt;subcategory;
      }
  
      public function setSubcategory(?DestinationSubcategory $subcategory): self
      {
          $this-&gt;subcategory = $subcategory;
  
          return $this;
      }
  }
  </code></pre>
  <p>Can someone point me to the right direction to solve this?</p>
  <p>Ps. I'm using symfony 5.0.7.</p>
  <p>Thanks in advance</p>