在字符串symfony 3.4上调用成员函数format()

时间:2018-10-08 14:55:30

标签: string symfony datetime format

发送电子邮件后,我试图在数据库中注册一个新约会,但它向我显示一个错误:在字符串上调用成员函数format(), 在vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Types \ DateTimeType.php 中 ,

因为在预订实体中,我需要注册的日期和时间是datetime类型。 这是预订实体:

 <?php

namespace Doctix\FrontBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**  
 * Booking
 *
 * @ORM\Table(name="booking")
 * 


 class Booking
{
  /**
   * @var int
   *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

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

/**
 * @var bool
 *
 *@ORM\Column(name="valider_rdv", type="boolean", nullable=true)
 */
private $validerRdv;

/**
 * @ORM\ManyToOne(targetEntity="Doctix\MedecinBundle\Entity\Medecin")
 * @ORM\JoinColumn(nullable=true)
 */
private $medecin;

/**
 * @ORM\ManyToOne(targetEntity="Doctix\PatientBundle\Entity\Patient")
 * @ORM\JoinColumn(nullable=true)
 */
private $patient;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set dateRdv
 *
 * @param \DateTime $dateRdv
 *
 * @return Booking
 */
public function setDateRdv($dateRdv)
{
    $this->dateRdv = $dateRdv;

    return $this;
}

/**
 * Get dateRdv
 *
 * @return \DateTime
 */
public function getDateRdv()
{
    return $this->dateRdv;
}

/**
 * Set heureRdv
 *
 * @param \DateTime $heureRdv
 *
 * @return Booking
 */
public function setHeureRdv($heureRdv)
{
    $this->heureRdv = $heureRdv;

    return $this;
}

/**
 * Get heureRdv
 *
 * @return \DateTime
 */
public function getHeureRdv()
{
    return $this->heureRdv;
}

/**
 * Set validerRdv
 *
 * @param boolean $validerRdv
 *
 * @return Booking
 */
public function setValiderRdv($validerRdv)
{
    $this->validerRdv = $validerRdv;

    return $this;
}

/**
 * Get validerRdv
 *
 * @return bool
 */
public function getValiderRdv()
{
    return $this->validerRdv;
}


 /**
 * Set medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 * @return Booking
 */
public function setMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin)
{
    $this->medecin = $medecin;

    return $this;
}

/**
 * Get medecin
 *
 * @return \Doctix\MedecinBundle\Entity\Medecin 
 */
public function getMedecin()
{
    return $this->medecin;
}

 /**
 * Set patient
 *
 * @param \Doctix\PatientBundle\Entity\Patient $patient
 * @return Booking
 */
public function setPatient(\Doctix\PatientBundle\Entity\Patient $patient)
{
    $this->patient = $patient;

    return $this;
}

/**
 * Get patient
 *
 * @return \Doctix\PatientBundle\Entity\Patient 
 */
public function getPatient()
{
    return $this->patient;
}
 }

这是控制器:

   public function patientHandleBookingAction(Request $request){

      $id = $request->query->get('id');
     $date = $request->query->get('date');
     $time = $request->query->get('time');
    // $user = $this->getUser();

    $em = $this->getDoctrine()->getManager();
    $repoPatient = $em->getRepository('DoctixPatientBundle:Patient');
    $patient = $repoPatient->findOneBy(array(
        'user' => $this->getUser()
    ));

    $repoMedecin = $em->getRepository('DoctixMedecinBundle:Medecin');
    $medecin = $repoMedecin->findOneBy(array(
        'id' => $request->query->get("idMedecin")));


    $mailer = $this->get('mailer');
    $message = (new \Swift_Message('Email de Confirmaton'))
        ->setFrom("medmamtest@gmail.com")
        ->setTo($patient->getUser()->getUsername())
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/registration.html.twig',
                array('name' => 'mam')
            ),
            'text/html'
        );

    $mailer->send($message);
    if($mailer){

      $booking = new Booking();
      $booking->setMedecin($medecin);
      $booking->setPatient($patient);
      $booking->setDateRdv('date');
      $booking->setHeureRdv('time');
      $booking->setValiderRdv(0);


    }

    $em->persist($booking);
    $em->flush();

    // A remplacer par un contenu plus approprié
    return $this->render('DoctixPatientBundle:Patient:confirm.html.twig',array(
            'time' => $request->query->get("time"),
            'date' => $request->query->get("date"),
            'medecin' => $medecin,
            'patient' => $patient,
           // 'date' => $date,
           // 'time' => $time
));

}

谢谢

1 个答案:

答案 0 :(得分:1)

发生此错误是因为您试图在预订实体上将日期和时间设置为字符串。

  $booking->setDateRdv('date');
  $booking->setHeureRdv('time');

尝试将其更改为:

  $booking->setDateRdv(new \DateTime($date));
  $booking->setHeureRdv(new \DateTime($time));