Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Doctrine Symfony4预科生不会开火_Doctrine_Symfony4 - Fatal编程技术网

Doctrine Symfony4预科生不会开火

Doctrine Symfony4预科生不会开火,doctrine,symfony4,Doctrine,Symfony4,我正在测试预科生和Symfony4和条令中的其他事件 这是我的控制器 <?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use App\Entity\Contact; class C00260CallbackController extends Ab

我正在测试预科生和Symfony4和条令中的其他事件

这是我的控制器

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Contact;
class C00260CallbackController extends AbstractController
{
    /**
     * @Route("/c00260/callback", name="c00260_callback")
     */
    public function index()
    // Callback statements are in the Entity definition
    {
        $entityManager = $this->getDoctrine()->getManager();
        $contact = new Contact();
        $contact->setName('Elodie');
        $contact->setEmail('elodie@email.com');
        //$contact->setTimestamp();
        $entityManager->persist($contact);
        $entityManager->flush();
                return $this->render('c00260_callback/index.html.twig', [
            'controller_name' => 'C00260CallbackController',
        ]);
    }
}

为什么要把它弄得这么复杂,为什么不在实体构造函数中设置datetime的默认值呢。这样,每个初始化的联系人都会自动获得一个日期,您可以随时轻松地覆盖它

如果您不希望这样,那么我建议单独设置一个实体侦听器,否则您的实体会变得过于臃肿,难以维护


希望我能帮上一点忙,如果你有问题的话,可以问一下。

也许你可以在实体的
\u构造()
方法中设置时间戳

__construct(){
$this->timestamp = new \Datetime();

}

试试这个,它对我来说非常好:
/**
*每次创建新对象时调用回调
*@ORM\PrePersist()
*@ORM\PreUpdate
*@返回响应
*/
公共职能预科生()
{
if(空($this->createdAt)){
$this->createdAt=new\DateTime();
}
}


然后把它放在你的实体中的参数后面

谢谢,我知道这一点。但是我的问题不是设置时间戳,而是测试prePersist事件处理。谢谢,我知道这一点。但我的问题不是设置时间戳,而是测试prePersist事件处理。
__construct(){
$this->timestamp = new \Datetime();