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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料