0

I constantly run into this and there has to be a better way to do it. So I have an Order Class and an Order_db class. in the order class there is a property $orderDate that is a DateTime object.

The problem is that sometimes the $orderDate is null. So it seems I have to options:

  1. When I do a select query I test to see if there was date before calling my setter. When I do an Insert query I check that $orderDate isn't null before trying to perform a ->format (as that would throw an error)

  2. I test in the setter if anything was passed before creating a new DateTime object but I'm still stuck with testing if it exists before using it with ->format.

It's not that big of a deal but I deal with a lot of date fields and it gets pretty repetitive. Is there a better way to handle this? In my ideal world if I passed nothing to DateTime when creating a instance it would be null, and if I called ->format on a null DateTime it would return nothing instead of throwing an error.

1 Answer 1

1

try this:

CREATE TABLE IF NOT EXISTS `DB`.`Exampletable` (
 `field_1` VARCHAR(255) NOT NULL DEFAULT '',
 `field_2` CHAR(20) NOT NULL default 'default_text',
 `field_3` INT(10) DEFAULT NULL,
 `field_4` BIGINT(20),
 `field_5` DATETIME DEFAULT NULL
);

Do you tried to add the Default NULL to the date's?

Sign up to request clarification or add additional context in comments.

4 Comments

I know how to do it. In fact you don't need to check empty just $orderDate ? doSomething : something else . But if you have tables with 4 date fields and you have 30 tables not to mention all the times it's used on a web page it gets really repetitive.
What about that? i cant tell whay tou know, and what you dont know... But you can tell us what have you tried before... @Casey
I did. In both options I talked about testing to make sure it wasn't null.
Have you tried a field with a int field, with the fields with null dates maybein binary, 4 date fields=4 bits 1001 (first and last are null) so you check only 1 field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.