2

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);
    }
}
YoannLth
  • 197
  • 2
  • 14
  • Please post the code where you are executing the query – Webeng Apr 23 '16 at 13:13
  • It's the `$this->getDb()->insert('event', $eventData);` – YoannLth Apr 23 '16 at 13:14
  • where is your $sql statement created? The big error that you posted in your question includes your SQL statement, and we have to see what values you are trying to bind to that statement in order to deduce the error. So if you can find the functions where you are binding the parameters to the (?, ?, ?.....) question marks, that code would be very helpful here – Webeng Apr 23 '16 at 13:17
  • I'm using Symfony & DBAL, I didn't write any SQL statement. `$this->getDb()->insert('event', $eventData);` replace the statement – YoannLth Apr 23 '16 at 13:21
  • What is the data type of startDate_event and endDate_event in SQL ? Also you're passing false as a endDate_event. – Alok Patel Apr 23 '16 at 13:22
  • And the value i'm trying to bind is `["rfreger", "gregregerg", 44, {"date":"2011-01-01 00:00:00","timezone_type":3,"timezone":"Europe/Paris"}, false, false, false, 2]` – YoannLth Apr 23 '16 at 13:22
  • @KyleCoupart Show us the Entity file of `event` entity. You can find it in Entity folder of the bundle. – Alok Patel Apr 23 '16 at 13:24
  • The data type of startDate_event is DateTime – YoannLth Apr 23 '16 at 13:27
  • http://piratepad.net/stack : full code – YoannLth Apr 23 '16 at 13:29
  • If you've used DateTime object then there is no need to use date_create_from_format() Correct your endDate function same as startDate,startHour and endHour. Also make sure you give some values to these columns as they are not null. Make sure they are DateTime objects. – Alok Patel Apr 23 '16 at 13:36
  • I didn't use date_create_from_format(), I tried to convert the DateTime to string before the SQL statement. It works but after, I have problems with my "edit" form. – YoannLth Apr 23 '16 at 13:44
  • My form need a DateTime object to generate the form... – YoannLth Apr 23 '16 at 13:46
  • The third paramenter of ->insert($table, $data, typeMapping) should be used, here to tell DBAL it's a DateTime on DB side. Rest will work out of the box, with \DateTime instances on the event array. – mblaettermann Apr 23 '16 at 21:06

1 Answers1

2

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!

Webeng
  • 7,050
  • 4
  • 31
  • 59