From f6bb0077428ae178eca41b02b896563941f735da Mon Sep 17 00:00:00 2001 From: Falmesino Abdul Hamid Date: Wed, 15 Feb 2023 03:38:31 +0700 Subject: [PATCH] fix: unable to save date into database fix "Object of class DateTime could not be converted to string" exception when saving a date into database --- src/EventSubscriber/DbTablePersister.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/DbTablePersister.php b/src/EventSubscriber/DbTablePersister.php index 49ec2b3..2fa4c80 100644 --- a/src/EventSubscriber/DbTablePersister.php +++ b/src/EventSubscriber/DbTablePersister.php @@ -5,6 +5,7 @@ namespace Bolt\BoltForms\EventSubscriber; use Bolt\BoltForms\Event\PostSubmitEvent; +use Carbon\Carbon; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Query\QueryBuilder; use Psr\Log\LoggerInterface; @@ -67,12 +68,19 @@ private function parseForm(Form $form, PostSubmitEvent $event, Collection $confi private function saveToTable(string $table, array $fields): void { $columns = []; + $parameters = []; foreach (array_keys($fields) as $name) { $columns[$name] = '?'; } - $parameters = array_values($fields); + foreach(array_values($fields) as $value) { + if ($value instanceof \DateTimeInterface) { + $parameters[] = Carbon::instance($value); + } else { + $parameters[] = $value; + } + } $this->query ->insert($table)