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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件