duanqu9279 2017-03-04 12:52
浏览 41
已采纳

PHP错误调用数组上的成员函数format()

I have a date time field

    /**
 * @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();
}

I this error below:

Call to a member function format() on array

The error on the page

Anyone know why I get this?

Edit:

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();
    }
  • 写回答

1条回答 默认 最新

  • dtdh11647 2017-03-04 19:27
    关注

    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();
        }
    

    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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决