简体   繁体   English

如何使用Doctrine和Symfony2调试失败的数据库写入?

[英]How do I debug a failing DB write with Doctrine and Symfony2?

In a controller I have this after a form is submitted and isValid() 在控制器中,提交表单和isValid()之后,我有了这个

// ...
$em = $this->getDoctrine()->getEntityManager();
$em->persist($form->getData());
$em->flush();
// ...

I have checked the contents of $form->getData() and it looks fine but it's not writing it to the table. 我已经检查了$form->getData()的内容,它看起来不错,但是没有将其写入表中。 I'm not getting any errors in the logs. 我的日志中没有任何错误。 How can I start debugging this? 我该如何开始调试呢?

in my Entity class, which was for user registration I had a date created field. 在我的Entity类(用于用户注册)中,我有一个创建日期字段。 In order to populate this field I had 为了填充此字段,我有

// this is the WRONG version
class User{

    //...
    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;


    //...
    public function __construct()
    {
        $this->created = date("Y-m-d H:i:s");
    }


    public function setCreated(\DateTime() $created)
    {
        $this->created = $created;
    }

}

However what I should have had is: 但是我应该有的是:

/** 
 * ... other orm settings
 * @ORM\HasLifecycleCallbacks()
 */
class User{

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;

    //....

    /**
     * @ORM\prePersist
     */
    public function setCreatedValue()
    {
        $this->created = new \DateTime();
    }

}

The reason I didn't spot this is that no error was logged to the log files. 我没有发现这一点的原因是没有错误记录到日志文件中。 I was submitting this form as an ajax call and didn't think to look in the response for that ajax call for the error until I turned on firebug (was relying on the default chrome console) 我将这个表单作为ajax调用提交,并且在我打开Firebug(依赖于默认的chrome控制台)之前,我不认为应该在该ajax调用的响应中查找错误。

I don't remember the exact error that can back but it was around the DateTimeType. 我不记得可以返回的确切错误,但它与DateTimeType有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何在Symfony2 / Doctrine中创建自定义EntityManager? - How exactly do I create a custom EntityManager in Symfony2/Doctrine? 如何在Symfony2中使用Doctrine进行多对多查询 - How can I do a manyTomany query with Doctrine in Symfony2 如何使用Doctrine为Symfony2建立数据库? - How do I set up a database for Symfony2 using Doctrine? Symfony2 Doctrine2如果不从数据库中获取数据,如何添加对象 - Symfony2 Doctrine2 how to add object if not fetched from db Symfony2 +原则OneToMany JOIN失败 - Symfony2 + Doctrine OneToMany JOIN failing 如何在 Doctrine 或 Symfony 中创建“一次写入”字段? - How do I create a "write once" field in Doctrine or Symfony? Symfony2 + Doctrine-fixtures + DB2:[Symfony \\ Component \\ Debug \\ Exception \\ ContextErrorException]警告:db2_bind_param():描述参数失败 - Symfony2 + Doctrine-fixtures + DB2: [Symfony\Component\Debug\Exception\ContextErrorException] Warning: db2_bind_param(): Describe Param Failed 我是否需要使用教义查询对Symfony2上的字符串进行转义? - Do I need to escape strings on Symfony2 with Doctrine queries? Symfony2,如何使用下拉列表在Doctrine中执行数据库检索? - Symfony2, How do I perform a database retrieve in Doctrine with a drop down list form? 如何配置在Symfony2项目中扩展PersistentObject的Doctrine2实体? - How do I configure a Doctrine2 entity which extends PersistentObject within Symfony2 project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM