0

I followed this question to create timestamps that save on create and update maintained by the database: Doctrine2 - Type timestamp - Default value Problem is that now I get a "value cannot be null" when saving a record using Doctrine. I see in the generated insert that it's trying to save the timestamps as null. Is there a way to avoid Doctrine setting a column on INSERT/UPDATE.

Community
  • 1
  • 1
Acuariano
  • 498
  • 3
  • 11

1 Answers1

0

Why do you want letting DB to do Doctrine job?

Doctrine won't set a column at all, if you don't map it. But would you take a look to Timestampable extension?

Another approach is using Doctrine entity listeners. So you can make your own, on PrePersist and PreUpdate and set the time there.

If you're worrying about setters for changing dates, you can at least use dynamic mapping for that fields (just map datetime properties on-the-fly when you really need to change them). Not sure that you can use reflection though, because your entity can be behind the proxy class.

If you still want keeping business-logic in Database, you can map only fields you really want to change (not datetime fields in your case), then write custom Doctrine hydrator (just extend their AbstractHydrator class) that will populate all the fields you need (include datetime). Also you can configure your new hydrator as default (i.e. in Doctrine configuration section in config.yml), so it will work without any adjustments.

E.K.
  • 1,045
  • 6
  • 10
  • Think of a single database used by multiple applications using different technologies. I want to rely on the database as much as possible since it simplifies consistency. – Acuariano May 09 '17 at 18:14
  • Ok, in that case Doctrine hydrators will help you. I've updated my answer (see the last paragraph) – E.K. May 09 '17 at 19:20