gpt4 book ai didi

php - 无法转换属性路径 : Expected a\DateTime or\DateTimeInterface 的值

转载 作者:可可西里 更新时间:2023-10-31 23:37:21 24 4
gpt4 key购买 nike

首先:我对 Symfony/编程还很陌生,在找到我需要的解决方案时遇到了问题。

我的“事件”实体中有这些变量,带有标准的 getter 和 setter。:

 /**
* @var \DateTime
*
* @ORM\Column(name="startTime", type="time", nullable=true)
*
* @Assert\Expression(
* "this.getStartTime() <= this.getEndTime()",
* message="Start time should be less or equal to end date!"
* )
*/
private $startTime;

/**
* @var \DateTime
*
* @ORM\Column(name="endTime", type="time", nullable=true)
*
* @Assert\Expression(
* "this.getEndTime() >= this.getStartTime()",
* message="End time should be greater or equal to start time!"
* )
*/
private $endTime;

我的表单有这个 Controller :

<?php


namespace AppBundle\Controller;


use AppBundle\Entity\Event;
use AppBundle\Form\EventCreateFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class EventController extends Controller {

/**
* @Route("/eventmanager", name="event")
*/
public function eventsAction(Request $request)
{
$events = $this->getDoctrine()// query database and get all events
->getRepository('AppBundle:Event')
->findAll();

$event = new Event();

$adminstatus = false;

if (in_array('ROLE_ADMIN', $this->getUser()->getRoles())) {
$adminstatus = true;
}
$event = $this->get('holiday.service')->createEventForm($request);
$form = $this->createForm(EventCreateFormType::class, $event, array('rolestatus' => $adminstatus));


if ($form->isValid()) {

$em = $this->getDoctrine()->getManager();
$em->persist($event);
$em->flush();

return $this->redirectToRoute('event'); // redirect to route: events
}

return $this->render('default/eventmanager.html.twig', array('events' => $events, 'form' => $form->createView()));
}

对于这种类型:

<?php


namespace AppBundle\Form;


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EventCreateFormType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);

$builder
->add('title', TextType::class, array(
'label' => 'event.form.title.label',
'attr' => array(
'maxlength' => 75,
'' => '')
))
->add('dateOf', TextType::class, array(
'label' => 'event.form.dateOf.label',
// 'widget' => 'single_text',
// 'html5' => false,
'attr' => [
'class' => 'js-datepicker',
// 'format' => 'dd-MM-yyyy',
],
))
->add('startTime', TimeType::class, array(
'label' => 'event.form.startTime.label',
))
->add('endTime', TimeType::class, array(
'label' => 'event.form.endTime.label',
))
->add('notice', TextareaType::class, array(
'label' => 'event.form.notice.label',
'required' => false,
'attr' => array('maxlength' => 2500)
));
if ($options['rolestatus'] == true) {
$builder->add('status', ChoiceType::class, array(
'label' => 'event.form.status.label',
'data' => $options['rolestatus'],
'choices' => array(
'event.form.status.public' => true,
'event.form.status.private' => false
),
'choices_as_values' => true
));
}
$builder->add('save', SubmitType::class, array(
'label' => 'event.form.create.button'
));


}

public function configureOptions(OptionsResolver $resolver)
{
$configs = $resolver->getDefinedOptions();
$configs[] = 'rolestatus';
$resolver->setDefined($configs);
}

还有这个服务:

<?php

namespace AppBundle\Controller\Services;

use AppBundle\Entity\Event;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class HolidayServiceController extends Controller {



public function createEventForm(Request $request) {

$submittedForm = $request->get('event_create_form');
$adminstatus = false;
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles())) {
$adminstatus = true;
}
dump($submittedForm);

$event = new Event();
if (!empty($submittedForm)) {

$creator = $this->getUser();
$title = $submittedForm['title'];
$startTime = $submittedForm['startTime'];
$endTime = $submittedForm['endTime'];
$notice = $submittedForm['notice'];
$now = new\DateTime();
$dateOf = new \DateTime();
$explodedDate = explode('-', $submittedForm['dateOf']);
$dateOf->setDate($explodedDate[2], $explodedDate[1], $explodedDate[0]);

$event->setTitle($title);
$event->setDateOf($dateOf);
$event->setStartTime($startTime);
$event->setEndTime($endTime);
$event->setNotice($notice);
$event->setCreator($creator);
$event->setCreatedAt($now);
if (!empty($submittedForm) && $adminstatus == true) {
$status = $submittedForm['status'];
$event->setStatus($status);
} else {
$event->setStatus(false);
}
return $event;
}
return $event;


}
}

现在,当我尝试创建一个新事件时,出现以下错误:

无法转换属性路径“startTime”的值:需要\DateTime 或\DateTimeInterface。500 内部服务器错误 - TransformationFailedException

我知道这是因为我传递的是 startTime 数组而不是对象。

我必须改变什么才能实现它?

提前致谢。

最佳答案

如果您使用 Symfony 表单,则不必“手动”从请求中检索数据。您唯一应该做的就是处理请求:

$event = new Exvent();
$form = $this->createForm(EventCreateFormType::class, $event);
$form->handleRequest($request);

处理请求只是将来自请求的数据与实体属性绑定(bind)。它从表单数组中获取值,查看属性应该是什么类型并将其转换为适当的格式。

如果您想手动执行此操作(那么您不必创建表单 - 它只是没用)您应该这样做:

$startTime = new \DateTime($submittedForm['startTime']);
$endTime = new DateTime($submittedForm['endTime']);

如您所说,如果 startTime 和 endTimes 是数组,则必须先从这些数组构建日期时间字符串,然后再将它们放入 DateTime 构造函数。

然后 startTime 和 endTime 将是 DateTime 类型。

关于php - 无法转换属性路径 : Expected a\DateTime or\DateTimeInterface 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40716070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com