【问题标题】:Symfony - DateTime as paramSymfony - 日期时间作为参数
【发布时间】:2023-04-06 05:52:01
【问题描述】:

我无法用 Symfony 定义我的日期时间参数。

如果 last_scan_date 为 null,我将尝试返回 null 如果不返回结果!

错误提示:

传递给 checkLastScan() 的参数 2 必须是 DateTime 的实例,给定字符串

我的功能:

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;
    }
}

和我的电话:

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

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

    return $this->success();

和实体:

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

也许问题是当需要像这样的帖子参数时:

/**
 * 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;
        }
    }

【问题讨论】:

  • 什么是 $this->data?
  • 受保护的 $data = [];这可以正常工作@GiacomoM
  • 我不确定它是否能正常工作:D 因为 last_scan_date 被视为字符串而不是日期
  • 我不确定,我不是 symfony 专家,但也许你需要在 $lastScanDate 成员的评论中添加@var date $lastScanDate。查看此帖子stackoverflow.com/questions/10836123/…
  • 我更新了答案,快来看看吧! @GiacomoM 谢谢。

标签: php doctrine symfony-3.4


【解决方案1】:

如果$this->data(我一开始问但你没有正确回答我)只是POST数组,当然数组的所有成员都被视为字符串。

您必须解析last_scan_date 字符串并将其转换为 DateTime 类型。

这是函数的代码(将 YOUR_POST_FORMAT 的值更改为您在 HTML 表单中使用的格式):

/**
 * 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;
        }
    }
}

【讨论】:

  • 这太棒了!非常感谢。 @GiacomoM
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多