Php 没有为约束Symfony\Component\Validator\Constraints\DateTime Symfony2配置默认选项

Php 没有为约束Symfony\Component\Validator\Constraints\DateTime Symfony2配置默认选项,php,symfony,datetime,Php,Symfony,Datetime,嗨,我有这个例外:没有为约束Symfony\Component\Validator\Constraints\DateTime配置默认选项 在我的控制器中,我有: use Symfony\Component\Validator\Constraints\DateTime; class DefaultController extends Controller { public function test2Action() { $em = $this->getDoctrine()-

嗨,我有这个例外:没有为约束Symfony\Component\Validator\Constraints\DateTime配置默认选项

在我的控制器中,我有:

use Symfony\Component\Validator\Constraints\DateTime;
class DefaultController extends Controller
{


public function  test2Action()
{

    $em = $this->getDoctrine()->getEntityManager();
    $id = 1; // ID du bureau de test que l'on a enregistré précédemment

    $desk = $this->getDoctrine()->getRepository('MytestBundle:Desk')->find($id);
    echo "Le bureau récupéré porte l'ID: ".$desk->getId()." et le titre: ".$desk->getTitle();

    $comment = new DeskComment();
    $comment->setDescrption("Mon premier commentaire: Joli bureau !");
    $comment->setSubmissionIp($this->getRequest()->server->get('REMOTE_ADDR'));
    $comment->setCreateAt(new DateTime('now'));
    $comment->setDesk($desk); // On lie le commentaire à notre bureau d'ID 1

    $em->persist($comment); // On persist le commentaire 1

    $comment2 = new DeskComment();
    $comment2->setDescrption("Mon deuxième commentaire: J'adore le bureau ! Bravo !");
    $comment2->setSubmissionIp($this->getRequest()->server->get('REMOTE_ADDR'));
    $comment->setCreateAt(new DateTime('now'));
    $comment2->setDesk($desk); // On lie le commentaire à notre bureau d'ID 1

    $em->persist($comment2); // On persist le commentaire 2

    $em->flush(); // On sauvegarde en BDD les deux commentaires

     return $this->render('MytestBundle:Default:test2.html.twig');
}
我认为问题在于新的日期时间“现在”

任何帮助

在DateTime new\DateTime'now'之前添加一个\。如果未使用\,则DateTime将被解释为Symfony\Component\Validator\Constraints\DateTime而不是PHP日期时间

您可以删除use Symfony\Component\Validator\Constraints\DateTime;之后。

在DateTime new\DateTime'now'之前添加一个\。如果未使用\,则DateTime将被解释为Symfony\Component\Validator\Constraints\DateTime而不是PHP日期时间


您可以删除use Symfony\Component\Validator\Constraints\DateTime;之后。

它适用于第一个日期,但不适用于第二个日期为什么???@user3816170您修改了2个日期时间?您必须删除use语句并在两个新日期之前添加\ Timelitz yes i modified the second date!!因为您使用的是$comment->setCreatedAt,而不是$comment2->setCreatedAt。瞧:它适用于第一个日期,但不适用于第二个日期。为什么@user3816170修改了2日期时间?您必须删除use语句并在两个新日期之前添加\ Timelitz yes i modified the second date!!因为您使用的是$comment->setCreatedAt,而不是$comment2->setCreatedAt。瞧