Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入今天日期原则_Php_Symfony_Date_Doctrine_Query Builder - Fatal编程技术网

Php 插入今天日期原则

Php 插入今天日期原则,php,symfony,date,doctrine,query-builder,Php,Symfony,Date,Doctrine,Query Builder,我已经创建了一个symfony项目,现在有一个日期问题。我想请求在数据库中插入当前日期。在我的实体中,我的字段是日期时间 存储库: /** * @ORM\Column(type="datetime", nullable=true) */ private $date_maj; 控制器: $qb = $this->getDoctrine() ->getManager() ->createQueryBuilder()

我已经创建了一个symfony项目,现在有一个日期问题。我想请求在数据库中插入当前日期。在我的实体中,我的字段是日期时间

存储库:

 /**
 * @ORM\Column(type="datetime", nullable=true)
 */
private $date_maj;
控制器:

 $qb = $this->getDoctrine()
        ->getManager()
        ->createQueryBuilder()
        ->update(Tache::class, 't')
        ->set('t.date_maj', date(('Y-m-d H:i:s'), time()))
        ->where('t.id = :id')
        ->setParameter('id', $id)
        ->getQuery()
        ->execute();
错误:

[Syntax Error] line 0, col 87: Error: Expected end of string, got '06'

您希望设置一个参数,其值为PHP
\DateTime
,类似于where子句中的
:id
。查询生成器将处理到数据库字符串值的转换:

$now = new \DateTime();
$qb = $this->getDoctrine()
    ->getManager()
    ->createQueryBuilder()
    ->update(Tache::class, 't')
    ->set('t.date_maj', ':now')
    ->where('t.id = :id')
    ->setParameter('now', $now)
    ->setParameter('id', $id)
    ->getQuery()
    ->execute();

您应该将实体与persist和flush一起使用,而不是使用queryBuilder。 您的实体看起来像这样:

/**
*@ORM\Column(type=“datetime”,nullable=true)
*@var DateTime |空
*/
私人$date_maj;
公共函数getDateMaj():?日期时间
{
返回$this->date_maj;
}
公共函数setDateMaj(?DateTime$dateMaj):self
{
$this->date_maj=$dateMaj;
}
和控制器端:

//例如,使用注入依赖性更好
$em=$this->getDoctrine()->getManager();
//获取您的实体(如果它来自表单,这一行是无用的,请从$Form->getData()检索实体)
$tache=$em->getRepository(tache::class)->find($id);
$now=new\DateTime();
$tache->setDateMaj($now);
$em->persist($tache);
$em->flush();

如果可以的话,您也可以编辑打字
DateTime
DateTimeInterface

您可以使用
new\DateTime()

public function __construct()
    {
        $this->date_maj=new \DateTime();
      
    }