Skip to content

Commit

Permalink
fix: unable to save date into database
Browse files Browse the repository at this point in the history
fix "Object of class DateTime could not be converted to string" exception when saving a date into database
  • Loading branch information
falmesino committed Feb 14, 2023
1 parent 41d5e37 commit f6bb007
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/EventSubscriber/DbTablePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f6bb007

Please sign in to comment.