2
Work on an application with PHP 5.5 + Symfony 2 + Doctrine.
In one of the tables of this application there is a field updated_at
which is initially NULL
. However, when I give one getUpdatedAt()
in this entity, I am returned an object of the type \DateTime
, and whose content is as follows::
object(DateTime)#1064 (3) {
["date"]=>
string(27) "-0001-11-30 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(17) "America/Sao_Paulo"
}
How to test, without too many tricks, whether this object was generated from a null date?
The problem is that it has already been created as a date! If you had one
DateTime::createFromFormat
internally would give to make a$date instanceof DateTime
to verify!– Wallace Maxters
Can’t you do a check of the pure attribute that comes from the bank to then check? something like
if ($data->updated_at !== NULL) { $data->getUpdatedAt() }
?– Wallace Maxters
@Wallacemaxters is a possibility, I can try to verify within the entity itself. But I didn’t want to have to do this with all classes that have a date field that can be null.
– Rodrigo Rigotti
Makes a
Trait
! You are using PHP 5.5. You can do a horizontal inheritance, where all the classes that implement thisTrait
will have this method with the checks. I already made it in theLaravel
– Wallace Maxters
that helps? [link]http://stackoverflow.com/questions/14504913/verify-valid-date-using-phps-datetime-class[/link]
– Samuel Diogo
I found a way, look at my answer. :)
– Rodrigo Rigotti