Estoy creando un formulario de ingreso de datos y tengo el siguiente error "Catchable Fatal Error: Object of class DateTime could not be converted to string in /var/www/abcis/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 103 ".
En el codigo del controller he setteado la fecha de creación a la fecha actual:
Código PHP:
/**
* Creates a new SchoolRecord entity.
*
* @Route("/create", name="admission_school_record_applicant_create")
* @Method("POST")
* @Template("ABCAdmissionBundle:Applicant:Applicant.html.twig")
*/
public function createAction(Request $request)
{
$session = $this->getRequest()->getSession();
$appId=$session->get('applicant');
$entity = new SchoolRecord();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager("admission");
$applicantObject = $em->getRepository('ABCAdmissionBundle:Applicant')->find($appId);
$entity->setApplicant($applicantObject);
$app=$entity->getApplicant();
$entity->setCreatedDate(new DateTime());
// $entity->setIsCurrent('true');
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
$session = $this->getRequest()->getSession();
$session->set('step',3);
$session->set('applicant',$app->getId());
return $this->redirect($this->generateUrl('admission_applicants'));
}
$session->set('step',2);
$session->set('applicant',$app->getId());
return $this->redirect($this->generateUrl('admission_applicants'));
/* return array(
'entity' => $entity,
'form' => $form->createView(),
);*/
}
En el codigo del formulario he comentado la linea del createdDate porque lo setteo en el controller
Código PHP:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('schoolName','text',array('label'=>'School name - Nombre del colegio *'))
->add('fromDate','date',array('format' => 'dd-MMM-yyyy',
'years'=>range((date("Y")-14), date("Y")),
'label'=>'Attended from - Periodo desde'))
->add('toDate','date',array('format' => 'dd-MMM-yyyy',
'years'=>range((date("Y")-14), date("Y")),
'label'=>'Attended to - Periodo hasta'))
->add('headName','text',array('label'=>'Name of the Head - Nombre del Director'))
->add('phoneNumber','text',array('label'=>'Phone number - Teléfono'))
->add('eMail','text',array('label'=>'E-mail - Correo electrónico'))
->add('isCurrent','choice',array(
'label'=>'Information of present school - Información del colegio actual *',
'empty_value' => 'Choose an option',
'choices'=> array(
'true' => 'Yes / Si',
'false' => 'No',
)))
// ->add('createdDate')
->add('address','entity',
array('label'=>'Select a address *',
'class'=>'ABCAdmissionBundle:Addresses',
'property'=>'id'
))
;
}
La vista la pinto de la siguiente manera:
Código HTML:
{% extends "ABCAdmissionBundle::layoutAppForm.html.twig" %}
{% block title %} School Information Form {% endblock %}
{% block contenido %}
{{form_start(form)}}
{{form_errors(form)}}
<div class="row-fluid">
<div class="span12">
{{form_row(form.schoolName, {'attr': {'class': 'span11'}}) }}
{{ form_row(form.headName, {'attr': {'class': 'span11'}}) }}
</div>
</div>
<div class="row-fluid">
<div class="span6">
{{form_label(form.fromDate)}}
{{form_widget(form.fromDate.day,{'attr': {'class': 'span3'}}) }}
{{form_widget(form.fromDate.month,{'attr': {'class': 'span3'}}) }}
{{form_widget(form.fromDate.year,{'attr': {'class': 'span3'}}) }}
{{form_row(form.phoneNumber)}}
{{form_row(form.address)}}
</div>
<div class="span6">
{{form_label(form.toDate)}}
{{form_widget(form.toDate.day, {'attr': {'class': 'span3'}}) }}
{{form_widget(form.toDate.month,{'attr': {'class': 'span3'}}) }}
{{form_widget(form.toDate.year,{'attr': {'class': 'span3'}}) }}
{{form_row(form.eMail)}}
{{form_row(form.isCurrent)}}
</div>
</div>
{{form_widget(form.save)}}
{{form_end(form)}}
{% endblock %}
Yo creo que mi problema es que no he convertido el Datetime a string, pero no se como hacerlo. De antemano les agradezco su ayuda.