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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀