所以我使用ajax将数据发送到控制器,我想将它插入注释表中;一切正常,如果我删除这两行关于插入注释的当前日期时间和设置我的实体可空 但是我如何插入当前日期时间;
控制器代码
if($request->get('texte')==NULL)
{
throw new AccessDeniedException('This user does not have access
to this section.');
}
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface)
{
throw new AccessDeniedException('This user does not have access
to this section.');
}
$comment = new Commentaire();
$em = $this->getDoctrine()->getManager();
$veterinaire=$em->getRepository("MyAppUserBundle:User")->findOneById($request->get('cible'));
$comment->setIdCible($veterinaire);
$comment->setIdClient($user);
$comment->setTexte($request->get('texte'));
$literalTime = \DateTime::createFromFormat("d/m/Y H:i",date_default_timezone_get());
$comment->setDate($literalTime);
$em->persist($comment);
$em->flush();
return new Response("");
}
我的实体:
/** * @var \DateTime * * @ORM\Column(name="date", type="datetime", nullable=false) */ private $date;
我甚至试图设置我的AppKernel __construct
public function __construct($environment, $debug)
{
date_default_timezone_set( 'Africa/Tunis' );
parent::__construct($environment, $debug);
}
答案 0 :(得分:0)
您只需将DateTime
对象传递给setDate
:
$comment->setDate(new \DateTime());
或者如果您想指定时区:
$comment->setDate(new \DateTime('now', new \DateTimeZone('Africa/Tunis')));
当然,您可以从DateTime
中提取setDate
对象的创建,并传递其他变量。