Php 对boolean上的成员函数format()的调用?
我有一个symfony3项目。我试图发布几条信息,包括两个日期。方法如下所示:Php 对boolean上的成员函数format()的调用?,php,symfony,Php,Symfony,我有一个symfony3项目。我试图发布几条信息,包括两个日期。方法如下所示: /** * @Route("/LegoPieces") * @METHOD("POST") * @View() * * @Annotations\QueryParam( * name="piece", nullable=false, description="piece" * ) * @Annotations\QueryParam( * name="type", nullable=false
/**
* @Route("/LegoPieces")
* @METHOD("POST")
* @View()
*
* @Annotations\QueryParam(
* name="piece", nullable=false, description="piece"
* )
* @Annotations\QueryParam(
* name="type", nullable=false, description="type"
* )
* @Annotations\QueryParam(
* name="startDate", nullable=false, description="Start Date YYYY-MM-DD HH:MM:SS (Should be in the future)"
* )
* @Annotations\QueryParam(
* name="endDate", nullable=false, description="End Date YYYY-MM-DD HH:MM:SS (Should be no more than 3 weeks in the future)"
* )
*/
public function postAction(ParamFetcherInterface $paramFetcher)
{
$piece = $paramFetcher->get('piece');
$type = $paramFetcher->get('type');
$start = $paramFetcher->get('startDate');
$end = $paramFetcher->get('endDate');
$startDate = DateTime::createFromFormat(' Y-m-d H:i:s', $start);
var_dump($startDate);
$endDate = DateTime::createFromFormat(' Y-m-d H:i:s', $end);
$em = $this->getDoctrine()->getManager('default');
$data = new LegoPieces();
$data->setPiece($piece);
$data->setType($type);
$data->setStartDate($startDate);
$data->setEndDate($endDate);
$em->persist($data);
$em->flush();
return new JsonResponse("LegoPieces Added Successfully", 200);
}
/**
* Set startDate
*
* @param \DateTime $startDate
*
* @return LegoPiece
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
二传手是这样的:
/**
* @Route("/LegoPieces")
* @METHOD("POST")
* @View()
*
* @Annotations\QueryParam(
* name="piece", nullable=false, description="piece"
* )
* @Annotations\QueryParam(
* name="type", nullable=false, description="type"
* )
* @Annotations\QueryParam(
* name="startDate", nullable=false, description="Start Date YYYY-MM-DD HH:MM:SS (Should be in the future)"
* )
* @Annotations\QueryParam(
* name="endDate", nullable=false, description="End Date YYYY-MM-DD HH:MM:SS (Should be no more than 3 weeks in the future)"
* )
*/
public function postAction(ParamFetcherInterface $paramFetcher)
{
$piece = $paramFetcher->get('piece');
$type = $paramFetcher->get('type');
$start = $paramFetcher->get('startDate');
$end = $paramFetcher->get('endDate');
$startDate = DateTime::createFromFormat(' Y-m-d H:i:s', $start);
var_dump($startDate);
$endDate = DateTime::createFromFormat(' Y-m-d H:i:s', $end);
$em = $this->getDoctrine()->getManager('default');
$data = new LegoPieces();
$data->setPiece($piece);
$data->setType($type);
$data->setStartDate($startDate);
$data->setEndDate($endDate);
$em->persist($data);
$em->flush();
return new JsonResponse("LegoPieces Added Successfully", 200);
}
/**
* Set startDate
*
* @param \DateTime $startDate
*
* @return LegoPiece
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
但是,每次我尝试发布数据时,都会收到以下错误消息:
"message": "Call to a member function format() on boolean",
"class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
我什么都试过了,什么都没用!谢谢你的帮助 您应该检查传入的日期。问题很可能是由于您使用了
DateTime::createFromFormat()
正如文件所说:
返回新的DateTime实例,失败时返回FALSE
因此,调用很可能失败,返回false
,稍后(可能在模板中)您(或在呈现日期时的细枝)在$startDate
或$endDate
上调用format()
,但其中一个实际上不是有效的日期时间对象
我的假设是,由于初始空白,格式“Y-m-dh:i:s”不正确?我认为问题的原因与@dbrumann所说的相同。但请尝试使用$startDate=\DateTime::createFromFormat()方法创建日期。当函数无法根据格式创建日期时,它将返回false,并且setter正在尝试将false设置到datetime对象中 在您发布的代码中,没有对
format()
的调用。您确定这就是错误发生的地方吗?请注意:您可以在setter中使用类型提示来防止这种情况发生,或者更确切地说,您的代码会更早失败,因为PHP会注意到您将布尔值而不是日期时间传递到setstardate(\DateTime$startDate)
Hi感谢您的帮助!我已经删除了空格,但没有任何更改-当我在$startDate之前添加\Datetime时,会收到一条错误消息。再次感谢!编写$startDate=\DateTime::createFromFormat('Y-m-dh:i:s',$start)时会出现什么样的错误
那么$start
的值是多少?结束日期相同?我得到了相同的错误!美元起始价是2017-12-22 13:26:47。谢谢回答得好,伙计——我有一个像“3-08-18”这样的约会,它抛出了同样的错误——帮助我解决了我的问题。谢谢