0

I have trouble defining my datetime param with Symfony.

I am trying to return null if last_scan_date is null and if not to return results!

Error says:

Argument 2 passed to checkLastScan() must be an instance of DateTime, string given

My function:

public function checkLastScan($hash, \DateTime $lastScanDate)
{
    $findLastScan = $this->getMyRepository()->findOneBy([
        'hash' => $hash,
        'lastScanDate' => $lastScanDate
    ]);

    if (!$findLastScan) {
        throw new \Exception('Not found!');
    }

    if ($lastScanDate === null) {
        return null;
    } else {
        return $findLastScan;
    }
}

and my call:

$this->requirePostParams(['hash', 'last_scan_date']);

$this->container->get('app')->checkLastScan(
        $this->data['hash'],
        $this->data['last_scan_date']
    );

    return $this->success();

And enitity:

/**
 * @ORM\Column(name="last_scan_date", type="date", nullable=true)
 */
private $lastScanDate;

Maybe problem is when requiring post params like:

/**
 * Require post params
 *
 * @param $params
 */
protected function requirePostParams($params)
{
    $currentRequest = $this->get('request_stack')->getCurrentRequest();

    $postData = $currentRequest->request->all();

    $postContent = json_decode($currentRequest->getContent(), true);

    if (!empty($postContent)) {
        $postData = $postContent;
    }

    $this->data = $postData;

    $missingParams = [];

    foreach ($params as $param) {
        if (!array_key_exists($param, $postData)) {
            $missingParams[] = $param;
        }
    }
Tommy J
  • 109
  • 9
  • what is $this->data? – Giacomo M Jul 31 '19 at 12:01
  • protected $data = []; That works as it should @GiacomoM – Tommy J Jul 31 '19 at 12:02
  • I am not sure it works as it should :D since last_scan_date is treated as string instead of date – Giacomo M Jul 31 '19 at 12:04
  • I am not sure about that, I am not expert with symfony, but maybe you need to add @var date $lastScanDate to the comment of $lastScanDate member. Check this post https://stackoverflow.com/questions/10836123/how-to-set-a-date-in-doctrine-2 – Giacomo M Jul 31 '19 at 12:06
  • I updated my answer, so take a look! @GiacomoM Thanks. – Tommy J Jul 31 '19 at 12:08
  • I have one problem with this code. When I leave empty param or if I want it to be null and to return null it throws "Could not convert PHP value '' of type 'string' to type 'date'. Expected one of the following types: null, DateTime" @GiacomoM – Tommy J Jul 31 '19 at 12:41
  • Thank you so much, bro. @GiacomoM – Tommy J Jul 31 '19 at 14:08

1 Answers1

0

If $this->data (I asked at first but you did not answer to me properly) is just the POST array, of course all members of the array are treated as string.

You have to parse last_scan_date string and transform it to DateTime type.

Here is the code of the function (change the value of YOUR_POST_FORMAT to the format you use in your HTML form):

/**
 * Require post params
 *
 * @param $params
 */
protected function requirePostParams($params)
{
    $currentRequest = $this->get('request_stack')->getCurrentRequest();

    $postData = $currentRequest->request->all();

    $postContent = json_decode($currentRequest->getContent(), true);

    if (!empty($postContent)) {
        $postData = $postContent;
    }

    // HERE YOU PARSE STRING TO DATETIME TYPE
    if (isset($postData['last_scan_date']) && !empty($postData['last_scan_date'])) {
        $postData['last_scan_date'] = DateTime::createFromFormat('YOUR POST FORMAT', $postData['last_scan_date'])
    } else {
        $postData['last_scan_date'] = null;
    }

    $this->data = $postData;

    $missingParams = [];

    foreach ($params as $param) {
        if (!array_key_exists($param, $postData)) {
            $missingParams[] = $param;
        }
    }
}
Giacomo M
  • 4,450
  • 7
  • 28
  • 57