dongzhe3171 2013-12-02 14:40
浏览 66
已采纳

PHP Doctrine:如何在insert / update上保留表默认值

I have a table with default values for some columns, i. e. a **creationDate** column that holds the creation date of the record and for that reason should only be set on initial creation but not on updating the record.
I would love to let mysql set the creation date but when inserting a record with doctrine this value would be overwritten with NULL even if I don't have a getter or setter within the entity class. The only way to prevent this is to also remove the corresponding (protected) property. But if I do so I can't access the value of the **creationDate** field at all.

If I let php create the current date and use this as the default it would also be set on update with the current timestamp. So it would be more an updateDate than a creationDate.

How can I make doctrine to omit these kind off fields when creating the insert/update sql without removing the properties within the entity class?

Would be thankful for any suggestion.

  • 写回答

1条回答 默认 最新

  • doupike2351 2013-12-02 14:47
    关注

    Set the creationDate in __construct. That way it would not be updated when you update your entity.

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

    <kbd>UPDATE (Raw SQL using Connection)</kbd>

    Question I answered that uses Raw SQL via Connection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器