Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OOP PHP – How to define DateTime property for a db table model object when there is a NULL value in database

I am defining a database model property like this.

class GuestList extends DatabaseModel
{
    protected static $table = 'GuestLists';

    public $ID;
    public $EventName;
    public \DateTime $EventDateTime;
   
}

But its throwing a fatal error. because there are already some data in that table and for some row the value in the EventDateTime is NULL. If i dont set \DateTime the error goes away. But is there any way so the fiels also accepts Null value?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just add nullable type hint by prefixing ? to your other type hint:

class GuestList extends DatabaseModel
{
    protected static $table = 'GuestLists';

    public $ID;
    public $EventName;
    public ?\DateTime $EventDateTime;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading