Search code examples
phpsymfonydatetimedbal

DBAL : save DateTime in DB


i'm trying to save DatTime object in MySQL DB. On insert, i have this error :

"An exception occurred while executing 'INSERT INTO event (name_event, desc_event, minimalPrice_event, startDate_event, endDate_event, startHour_event, endHour_event, num_ET) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["rfreger", "gregregerg", 44, {"date":"2011-01-01 00:00:00","timezone_type":3,"timezone":"Europe/Paris"}, false, false, false, 2]: Catchable Fatal Error: Object of class DateTime could not be converted to string"

I don't understand why the method try to convert the DateTime object to a String

how to fix this problem?

public function save(Event $event) {
    $eventData = array(
        'name_event' => $event->getName(),
        'desc_event' => $event->getDesc(),
        'minimalPrice_event' => $event->getMinimalPrice(),
        'startDate_event' => $event->getStartDate(),
        'endDate_event' => $event->getEndDate(),
        'startHour_event' => $event->getStartHour(),
        'endHour_event' => $event->getEndHour(),
        'num_ET' => $event->getType()
        );

    if ($event->getNum()) {
        // The event has already been saved : update it
        $this->getDb()->update('event', $eventData, array('num_event' => $event->getNum()));
    } else {
        // The event has never been saved : insert it
        $this->getDb()->insert('event', $eventData);
        // Get the id of the newly created event and set it on the entity.
        $id = $this->getDb()->lastInsertId();
        $event->setNum($id);
    }
}

Solution

  • With MySQL, a string called an SQL statement is first created that will then be sent to the database to be executed. The SQL statement your code created was something along the lines of:

    INSERT INTO event (name_event, desc_event, minimalPrice_event, startDate_event, endDate_event, startHour_event, endHour_event, num_ET) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    

    In php, you are able to join strings together to create a longer string like so:

    $stringFinal = $stringA."other string".$stringB.$number4;
    

    And you can even add numbers in there. However there are certain DataTypes that just won't convert to strings. An array is a perfect example. If you have the following array:

    $myArray = array (
      [0] => 4,
      [44] => 'hello'
    )
    

    and if you tried to make a string with the following code:

    $myString = "hello".$myArray;
    

    The server running the script will get completely confused because it was not designed to convert an array into a string.

    So answering your doubt:

    I don't understand why the method try to convert the DateTime object to a String

    It needs to be a string because that is the format the database reads when you first query your SQL command.

    And the reason you are having the error is because there is a variable (or more than one variable) that you are passing in your code to be INSERTED into your database that can't be converted into a string.

    I hope that helps!