我在Silex中有一个带有'datetime'字段的表单。提交表单(保存到数据库)时,它返回错误如下:
捕获致命错误:第754行的/home/(...)/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php中无法将类DateTime的对象转换为字符串
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);
}
}
Events.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);
}
}
}
如何解决此错误?
答案 0 :(得分:2)
使用doctrine orm
或添加数据转换器以形成http://symfony.com/doc/current/cookbook/form/data_transformers.html
或者在保存
时手动转换此字段public function save($event)
{
$event['startDate'] = $event['startDate']->format($this->db->getDatabasePlatform()->getDateTimeFormatString());
...