j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Error with null dates #181

Closed DavidePastore closed 10 years ago

DavidePastore commented 10 years ago

I have a database with 'birth_date' column, which can be NULL or a date.

If I use plain 'echo' to output birth_day, it works in either case - as null and as a date.

If however I'm using Twig as a template engine and accessing birthday through Twig, it works ONLY when there is a date. If there is NULL, then application crashes and browser gives me back ERR_EMPTY_RESPONSE. Nothing more.

I'm pretty sure the problem occurs somewhere in the ORM, but I can't figure out how to solve it. I'd like to have just and empty string outputed in Twig if there's a NULL in db.

This isn't a problem of Twig because if I pass to it a null value, it simply doesn't print it.

tag commented 10 years ago

NULL and empty string are different, so this wouldn't be appropriate, I think.

One way to handle it is to add NOT NULL DEFAULT '' to your column definition in your database, so that you prevent NULLs on the data side.

DavidePastore commented 10 years ago

@tag: The problem is that the field is of DATE type. If I set it to a NOT NULL DEFAULT '' it will set to the 00:00:00 value.

treffynnon commented 10 years ago

There won't be any casting on NULL to an empty string for the reasons @tag states.

I am not sure why there is an issue with Twig, but a number of users have reported it as being a recursive call triggering it.

Unfortunately I have never experienced this bug despite using both Idiorm and Twig in projects so I have been unable to observe it.

DavidePastore commented 10 years ago

I'll try to create a simple project where the problem arises.

tag commented 10 years ago

I use Twig as well, and for various design reasons haven't encountered this error. Off the top of my head, I wonder whether a custom Twig filter might help with the issue as well.

DavidePastore commented 10 years ago

This is a thread where there is the same problem.

This solution is a little forced.

The real problem is that the application crashes when you try to access to that field via twig. Even a simple check to see if the variable is defined leads to a crash.

I created this project to reproduce the same issue. Check it and the instructions in README.md.

nicolas-t commented 10 years ago

Hi,

Same issue here :(

treffynnon commented 10 years ago

Thank you for all the effort you have put in here. Does anyone have any time to put together a pull request that fixes this issue? I am struggling to find the time to test this out.