2
フィールド 'datetime'を持つSilexにフォームがあります。フォームが送信された場合(データベースに保存)、それは以下のようにエラーを返します:SILEX PHPフォーム - DateTimeクラスのオブジェクトを文字列に変換できません
Catchable fatal error: Object of class DateTime could not be converted to string in /home/(...)/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 754
EventType.php
<?php
/**
* Event type.
*
*/
namespace Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Class EventType.
*
* @package Form
*/
class EventType extends AbstractType
{
/**
* Form builder.
*
* @param FormBuilderInterface $builder Form builder
* @param array $options Form options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'id',
'hidden'
);
$builder->add(
'name',
'text',
array(
'label' => 'event.name',
'required' => true,
)
);
$builder->add(
'startDate',
'date',
array(
'label' => 'event.startDate',
'required' => true,
)
);
$builder->add(
'endDate',
'date',
array(
'label' => 'event.endDate',
)
);
$builder->add(
'startHour',
'time',
array(
'label' => 'event.startHour',
)
);
$builder->add(
'endHour',
'time',
array(
'label' => 'event.endHour',
)
);
$builder->add(
'periodicity',
'number',
array(
'label' => 'event.periodicity',
)
);
}
/**
* Getter for form name.
*
* @return string Form name
*/
public function getName()
{
return 'event_form';
}
}
EventController
<?php
/**
* Event controller.
*
*/
namespace Controller;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Model\Events;
use Form\EventType;
/**
* Class EventController.
*
* @package Controller
*
*/
class EventController implements ControllerProviderInterface
{
/**
* Routing settings.
*
* @param Silex\Application $app Silex application
* @return Silex\ControllerCollection Result
*/
public function connect(Application $app)
{
$eventController = $app['controllers_factory'];
$eventController->get('', array($this, 'indexAction'))->bind('events');
$eventController->get('/', array($this, 'indexAction'));
$eventController->match('/add', array($this,'addAction'))->bind('events-add');
$eventController->match('/add/', array($this,'addAction'));
//$eventController->match('/edit/{id}', array($this, 'editAction'))->bind('event-edit');
//$eventController->match('/delete/{id}', array($this, 'deleteAction'))->bind('event-delete');
return $eventController;
}
/**
* Index action.
*
* @param Silex\Application $app Silex application
* @param Symfony\Component\HttpFoundation\Request $request Request object
* @return string Response
*/
public function indexAction(Application $app, Request $request)
{
$view = array();
//$events = new Events();
//$view['events'] = $events->findAll();
return $app['twig']->render('Event/index.html.twig', $view);
}
/**
* Add action.
*
* @param Silex\Application $app Silex application
* @param Symfony\Component\HttpFoundation\Request $request Request object
* @return string Response
*/
public function addAction(Application $app, Request $request)
{
$view = array();
$addEventForm = $app['form.factory']
->createBuilder(new EventType(), array())->getForm();
$addEventForm->handleRequest($request);
if ($addEventForm->isValid()) {
$eventData = $addEventForm->getData();
$eventModel = new Events($app);
$eventModel->save($eventData);
return $app->redirect(
$app['url_generator']->generate('events'),
301
);
}
$view['form'] = $addEventForm->createView();
return $app['twig']->render('Event/add.html.twig', $view);
}
}
イベント。 PHP(モデル)
<?php
/**
* Events model.
*
*/
namespace Model;
use Silex\Application;
/**
* Class Events.
*
* @package Model
* @use Silex\Application
*/
class Events
{
/**
* Db access object.
*
* @var Silex\Provider\DoctrineSeviceProvider $db
*/
private $db = null;
/**
* Events constructor.
*
* @param Application $app Silex application objecy
*/
public function __construct(Application $app)
{
$this->db = $app['db'];
}
/**
* Find all events.
*
* @return array Result
*/
public function findAll()
{
$query = 'SELECT * FROM wydarzenie';
$result = $this->db->fetchAll($query);
return !$result ? array() : $result;
}
/**
* Gets single event.
*
* @param integer $id Record Id
* @return array Result
*/
public function find($id)
{
if ($id != '' && ctype_digit((string)$id)) {
$query = 'SELECT * FROM wydarzenie WHERE id= :id';
$statement = $this->db->prepare($query);
$statement->bindValue('id', $id, \PDO::PARAM_INT);
$statement->execute();
$result = $statement->fetchAll(\PDO::FETCH_ASSOC);
return !$result ? array() : current($result);
} else {
return array();
}
}
/**
* Save event.
*
* @param array $event Event data
* @return mixed Result
*/
public function save($event)
{
if(isset($event['id']) && $event['id'] != '' && ctype_digit((string)$event['id'])) {
$id = $event['id'];
unset($event['id']);
return $this->db->update('wydarzenie', $event, array('id' => $id));
} else {
return $this->db->insert('wydarzenie',$event);
}
}
}
このエラーを修正するにはどうすればよいですか?