title: Persisting object to database error
body: <p>I have this error:</p>
  
  <pre><code>Expected argument of type "integer or null", "App\Entity\User" given
  </code></pre>
  
  <p>which I don't understand, well I understand the error, but I'm not sure why it's arising.</p>
  
  <p>Here's my AddController.php file:</p>
  
  <pre><code>&lt;?php
      namespace App\Controller\Tag;
  
      use App\Entity\Tag;
      use App\Form\Tag\AddType;
      use Symfony\Bundle\FrameworkBundle\Controller\Controller;
      use Symfony\Component\Config\Definition\Exception\Exception;
      use Symfony\Component\HttpFoundation\Request;
  
      class AddController extends Controller
      {
          public function add(Request $request)
          {
              $tag  = new Tag();
              $form = $this-&gt;createForm(AddType::class, $tag);
  
              $form-&gt;handleRequest($request);
  
              if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
                  $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
  
                  $pageInfo = $form-&gt;getData();
  
                  $description = $pageInfo['description'];
                  $name        = $pageInfo['name'];
                  $guru        = $pageInfo['guru_id'];
                  $createdTs   = new \DateTime();
  
                  $tag-&gt;setApproved(false);
                  $tag-&gt;setDescription($description);
                  $tag-&gt;setName($name);
                  $tag-&gt;setGuruId((is_int($guru) ? $guru : null));
                  $tag-&gt;setCreatedTs($createdTs);
  
                  try {
                      $entityManager-&gt;persist($tag);
                      $entityManager-&gt;flush();
  
                      $this-&gt;addFlash('success', 'Tag Submitted for review! '. $guru);
                  } catch (Exception $e) {
                      $this-&gt;addFlash('danger', 'Something went skew-if. Please try again.');
                  }
  
                  return $this-&gt;redirectToRoute('tag_add');
              }
  
              return $this-&gt;render('tag/add.html.twig', array('form' =&gt; $form-&gt;createView()));
          }
      }
  </code></pre>
  
  <p>the initial load works, but submitting the form generates the above error. But I'm not sure why. Even if I reorder my lines like this:</p>
  
  <pre><code>$tag  = new Tag();
  $tag-&gt;setGuruId(null);
  </code></pre>
  
  <p>my Tag entity:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity(repositoryClass="App\Repository\TagRepository")
   */
  class Tag
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=125)
       */
      private $name;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       */
      private $description;
  
      /**
       * @ORM\Column(type="boolean")
       */
      private $approved;
  
      /**
       * @ORM\Column(type="datetime")
       */
      private $created_ts;
  
      /**
       * @ORM\Column(type="datetime", nullable=true)
       */
      private $last_edit_ts;
  
      /**
       * @ORM\Column(type="integer", nullable=true)
       */
      private $guru_id;
  
      public function getId()
      {
          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 getDescription(): ?string
      {
          return $this-&gt;description;
      }
  
      public function setDescription(?string $description): self
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      public function getApproved(): ?bool
      {
          return $this-&gt;approved;
      }
  
      public function setApproved(bool $approved): self
      {
          $this-&gt;approved = $approved;
  
          return $this;
      }
  
      public function getCreatedTs(): ?\DateTimeInterface
      {
          return $this-&gt;created_ts;
      }
  
      public function setCreatedTs(\DateTimeInterface $created_ts): self
      {
          $this-&gt;created_ts = $created_ts;
  
          return $this;
      }
  
      public function getLastEditTs(): ?\DateTimeInterface
      {
          return $this-&gt;last_edit_ts;
      }
  
      public function setLastEditTs(\DateTimeInterface $last_edit_ts): self
      {
          $this-&gt;last_edit_ts = $last_edit_ts;
  
          return $this;
      }
  
      public function getGuruId(): ?int
      {
          return $this-&gt;guru_id;
      }
  
      public function setGuruId(?int $guru_id): self
      {
          $this-&gt;guru_id = $guru_id;
  
          return $this;
      }
  }
  </code></pre>
  
  <p>my AddType</p>
  
  <p>
  
  <pre><code>use App\Entity\Tag;
  use App\Entity\User;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class AddType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('name', TextType::class)
              -&gt;add('description', TextareaType::class)
              -&gt;add('guru_id', EntityType::class, array(
                  'class' =&gt; User::class,
                  'choice_label' =&gt; 'username'
              ));
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; Tag::class
          ));
      }
  }
  </code></pre>
  
  <p>it still gives the same error so I'm really not sure how it's being generated... how can I persist an integer (got from EntityType dropdown) to database?</p>
  
  <p>Thanks</p>