title: Symfony 4, Argument 1 passed to .. must be an instance of DateTime, null given
body: <p>My question is very related to <a href="https://stackoverflow.com/questions/48826086/symfony-3-4-type-error-argument-1-passed-to-must-be-an-instance-of-datetime">this question</a> but not quite the same. It's a follow up question on <a href="https://stackoverflow.com/questions/56612348/how-to-join-multiple-entities-on-a-foreign-id-in-symfony-4-using-a-query-builder">my previous one</a> if you have read it but it doesn't depend on it that much, so reading this question only should be enough. I have this method to save a 'touch' on a 'cabin' (I know it's odd). I (try to) do so be extracting information from a POST request, and test it using POSTMAN.</p>
  
  <pre class="lang-php prettyprint-override"><code>
  /**
   * @param Request $request
   * @throws \Doctrine\ORM\ORMException
   * @throws \Doctrine\ORM\OptimisticLockException
   * @throws \ErrorException
   * @return JsonResponse
   */
  public function registerTouch(Request $request)
  {
      $touchService = new TouchService($this-&gt;entityManager);
  
      $cabinet = $request-&gt;get('cabinet_id');
  
      /**
       * @var $touch Touch
       */
      $touch = new Touch(
          $request-&gt;get('time'),
          $request-&gt;get('toucher'),
          $request-&gt;get('cabinet_id'),
          $request-&gt;get('id')
      );
  
      if (empty($cabinet)) {
          return new JsonResponse(['error' =&gt; 'Touch not saved'], 200);
      } else {
          $touch-&gt;setCabinet($cabinet);
          $touchService-&gt;registerTouch($touch);
          return new JsonResponse(['success' =&gt; 'Touch saved'], 200);
      }
  </code></pre>
  
  <p>and the Touch class consists of the following:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Entity;
  
  use DateTime;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity(repositoryClass="App\Repository\TouchRepository")
   */
  class Touch implements \JsonSerializable
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="datetime")
       */
      private $time;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $toucher;
  
      private $accountId;
  
      /**
       * @ORM\ManyToOne(targetEntity="Cabinet")
       */
      private $cabinet;
  
      /**
       * Touch constructor.
       * @param DateTime $time
       * @param string $toucher
       * @param Cabinet $cabinet
       * @param int $id
       */
      public function __construct(DateTime $time, string $toucher, Cabinet $cabinet = null, int $id = null)
      {
          $this-&gt;time = $time;
          $this-&gt;toucher = $toucher;
          $this-&gt;cabinet = $cabinet;
          $this-&gt;id = $id;
      }
  
      public function getId(): int
      {
          return $this-&gt;id;
      }
  
      public function setId(int $id): self
      {
          $this-&gt;id = $id;
  
          return $this;
      }
  
      public function getTime(): DateTime
      {
          return $this-&gt;time;
      }
  
      public function getToucher(): string
      {
          return $this-&gt;toucher;
      }
  
      public function getCabinet(): Cabinet
      {
          return $this-&gt;cabinet;
      }
  
      public function setCabinet(Cabinet $cabinet): self
      {
          $this-&gt;cabinet = $cabinet;
          $this-&gt;accountId = $cabinet-&gt;getId();
          return $this;
      }
  
      public function getAccountId(): int
      {
          return $this-&gt;accountId;
      }
  
      public function setAccountId(int $accountId): self
      {
          $this-&gt;accountId = $accountId;
  
          return $this;
      }
  
      public function jsonSerialize()
      {
          return get_object_vars($this);
      }
  }
  
  </code></pre>
  
  <p>But when running this code I'm getting this error:</p>
  
  <blockquote>
    <p>Argument 1 passed to App\Entity\Touch::__construct() must be an instance of DateTime, null given, called in /var/www/learningProject/src/Controller/APITouchController.php on line 89 (500 Internal Server Error)</p>
  </blockquote>
  
  <p>I'm passing the data using POSTMAN:</p>
  
  <pre><code>[
      {
          "id": 666,
          "cabinet_id": 55,
          "time": {
              "date": "2018-06-18 11:51:22.000000",
              "timezone_type": 3,
              "timezone": "UTC"
          },
          "toucher": "person1",
      }
  ]
  </code></pre>
  
  <p>Which is the correct layout for a DateTime object so I'm not sure why this error is happening nor how to solve it. Any help would be appreciated!</p>