PHP 错误调用数组上的成员函数 format() [英] PHP Error Call to a member function format() on array

查看:27
本文介绍了PHP 错误调用数组上的成员函数 format()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期时间字段

    /**
 * @var \DateTime
 *
 * @ORM\Column(name="date", type="datetime", nullable=true)
 */
private $datetime;  

/**
 * Set date
 *
 * @param \DateTime $datetime
 *
 */
public function setDate($datetime)
{
    $this->datetime = $datetime;

    return $this->datetime ?? new \DateTime();
}

/**
 * Get date
 *
 * @return \DateTime
 */
public function getDate(): \DateTime
{
    return $this->datetime ?? new \DateTime();
}

我在下面这个错误:

在数组上调用成员函数 format()

Call to a member function format() on array

有人知道我为什么会得到这个吗?

Anyone know why I get this?

下面是我用来生成一个表单以读取 DateTime 值,然后从表单中收集数据并在表中创建一个新实体的代码:

Below is the code that I use to generate a form to read in the DateTime value and then collect the data from the form and create a new entity in the table:

$trainingform = new Training();

    $form = $this->createFormBuilder($trainingform)
        ->add('Leader', TextType::class)
        ->add('Date', DateTimeType::class, ['label' => 'Date and Time'])
        ->add('topics', TextType::class, ['label' => 'Topics Being Covered'])
        ->getForm();

    if ($form->handleRequest($request)->isValid()) {
        $trainingform->setLeader($request->request->get('form')['Leader']); 
        $trainingform->setDate($request->request->get('form')['Date']);
        $trainingform->setTopics($request->request->get('form')['topics']);
        $em->persist($trainingform);
        $em->flush();
    }

推荐答案

要正确处理您的数据,您应该使用 symfony 表单的 getData-方法,它将您的请求转换为您指定的状态在你之前的表格中.在您的情况下,它应该如下所示:

To handle your data correctly you should use symfony form's getData-method, which will transform your request to the state you've specified in your form before. In your case it should look like this:

    $trainingForm = new Training();

    $form = $this->createFormBuilder($trainingForm)
        ->add('Leader', TextType::class) // 'Leader' should be named as your property
        ->add('datetime', DateTimeType::class, ['label' => 'Date and Time']) //changed your Date to datetime as your property is
        ->add('topics', TextType::class, ['label' => 'Topics Being Covered'])  //should be named as your property as well
        ->add('submit', SubmitType::class, ['label' => 'Submit form']) //should be there
        ->getForm();

    $form->handleRequest($request)

    if (form->isSubmitted() && form->isValid()) {
        $trainingForm= $form->getData();
        $em->persist($trainingForm);
        $em->flush();
    }

为了避免这种不一致,您可以在 setter 方法中指定所需的类型:

To avoid such inconsistency, you could specify the required type on your setter method:

/**
 * Set date
 *
 * @param \DateTime $datetime
 */
public function setDate(\DateTime $datetime) //typhint added
{
    $this->datetime = $datetime;
}

`

Setter 不应该返回任何东西.然后你会在你可以控制的步骤上得到你的错误,你就会知道应该在哪里挖掘.

Setter shouldn't return anything btw. Then you would get your error on the step you can control and you would know where you should dig after.

这篇关于PHP 错误调用数组上的成员函数 format()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆