注意:类DateTime的对象无法转换为int-Symfony2
我想在创建操作期间获取并保存两次的总小时数 亚马尔 实体注意:类DateTime的对象无法转换为int-Symfony2,symfony,datetime,doctrine-orm,Symfony,Datetime,Doctrine Orm,我想在创建操作期间获取并保存两次的总小时数 亚马尔 实体 private $timein;//DateTime private $timeout;//DateTime private $total;//DateTime /** * Get total * * @return \DateTime */ public function getTotal() { return $this->total; } /** * Set total * * @param
private $timein;//DateTime
private $timeout;//DateTime
private $total;//DateTime
/**
* Get total
*
* @return \DateTime
*/
public function getTotal()
{
return $this->total;
}
/**
* Set total
*
* @param \DateTime $total
*
* @return Timerecord
*/
public function setTotal($total)
{
$this->total = $total;
return $this;
}
public function getWorkedHoursTimeinTimeout()
{
if (!empty($this->timein) && !empty($this->timeout)) {
//if already timed in and timed out, calculate hours from timein to timeout
$start = $this->timein;
$end = $this->timeout;
$start1 = $start;
$end1 = $end;
$total_hours = round(($end1-$start1) / 3600);//option1
return $total_hours;
} /*else {
$this->total = "";
}*/
}
现在在控制器中,我想保存timeim和timeout的总小时数,如下所示
$entity->setTotal($entity->getWorkedHoursTimeinTimeout());
$em->persist($entity);
$em->flush();
但我犯了这个错误
注意:类DateTime的对象无法转换为int
$timein、$total和$timeout实际上是doctor.yml中的时间类型,但在运行时会自动转换为DateTime
php app/console doctrine:generate:entities
在Mysql中,$timein和$timeout是时间类型
现在,当我重构getWorkedHoursTimeinTimeout()时
此返回无错误,但不保存实际总小时数,而是将保存值
00:00:00
现在我尝试返回并保存$timein或$timeout的值
if (!empty($this->timein) && !empty($this->timeout)) {
//if already timed in and timed out, calculate hours from timein to timeout
$start = $this->timein;
$end = $this->timeout;
$start1 = $start;
$end1 = $end;
$total_hours = $end1->diff($start1);//option1
return $start;//02:00:04
} /*else {
它实际上保存到Mysql的total列中
有什么想法吗?
Total
是DateTime对象
,您可以尝试保存DateInterval对象
。将实体中的Total
的类型更改为dateinterval您能给我举个例子吗?当您转储DateTime对象时,这将返回:Object(DateTime){[“date”]=>string(26)“2016-01-27 02:00:00.000000”[“timezone\u type”]=>int(3)[“timezone”]=>string(13)“欧洲/柏林”}
而这当日期间隔对象
时:对象(日期间隔){780(15){“y”=>int(0)[“m”]=>int(0)[“d”]=>int(0)[“h”=>int(1)[“i”]=>int(0)[“s”]=>int(0)][“工作日”=>int(0)[“工作日行为”]=>int(0)][“第一个工作日”=>int(0)][“第一个工作日”=>int(0)”的最后一天”=>int(0)=>int(0)[/code>=“datetime”)
到@ORM\Column(type=“dateinterval”)
我尝试了你的建议,但Symfony2抱怨日期间隔的列类型未知。你有哪个Symfony版本?我使用的是2.7版本
00:00:00
if (!empty($this->timein) && !empty($this->timeout)) {
//if already timed in and timed out, calculate hours from timein to timeout
$start = $this->timein;
$end = $this->timeout;
$start1 = $start;
$end1 = $end;
$total_hours = $end1->diff($start1);//option1
return $start;//02:00:04
} /*else {