单击symfony中的按钮后如何更改布尔值

时间:2018-10-24 14:40:27

标签: symfony boolean

在这里我的数据库中有一个实体validateRdv,它是一个布尔值,当患者与医生预约时,将其初始化为零。在医生必须单击批准按钮批准约会之后。当医生单击批准的按钮时,我希望将布尔validateRdv的值设置为1并向他批准任命的患者发送电子邮件。使用我的控制器,我尝试更改validateRdV属性的值,但无法执行。 也许是我做错了。如果有人可以帮助我

控制器

  public function approuverRdvAction(Request $request)
{
    // $id = $request->query->get('id');

    $em = $this->getDoctrine()->getManager();
    $repos = $em->getRepository("DoctixFrontBundle:Booking");
    $bookings = $repos->findOneBy(array('id' => $request->query->get("idBooking")));

    $booking = new Booking();
    $booking = $bookings;
    $booking->setValiderRdv();

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

    $mailer = $this->get('mailer');
    $message = (new \Swift_Message('Votre Rdv est approuvé par le médecin'))
        ->setFrom("medmamtest@gmail.com")
        ->setTo($bookings->getPatient()->getUser()->getUsername())
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/confirmation.html.twig',
                array('name' => 'Cher(s)')
            ),
            'text/html'
        );

    $mailer->send($message);

    return $this->render('DoctixMedecinBundle:Medecin:bookingConfirm.html.twig', array(
            'bookings' => $bookings
    ));
}

出现错误:在null上调用成员函数setValiderRdv()

实体

  <?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;
}

}

路由

 medecin_booking_approuver:
     path: /medecin/booking/approuver
     defaults: { _controller: DoctixMedecinBundle:Medecin:approuverRdv}

谢谢

2 个答案:

答案 0 :(得分:0)

首先,我不得不说您不需要实例化新的Booking来覆盖它,因为findOneBy()将返回该对象的实例。 关于这个问题,似乎您没有正确地使用“ idBooking”,因为findOneBy()无法找到具有该ID的对象,因此建议您找出要获取的值。 还请记住,setValiderRdv()需要一个参数(必须为布尔值),因此该行必须为:

$booking->setValiderRdv(true);

希望有帮助。

答案 1 :(得分:0)

您可以定义您的路由等待一个特定参数id,与占位符几乎相同。

medecin_booking_approuver:
     path: /medecin/booking/{id}/approuver
     defaults: { _controller: DoctixMedecinBundle:Medecin:approuverRdv}

此后,您只需typehint对Booking类进行操作即可。 Symfony(感谢param converter)将从数据库中检索URL中ID的预订实体。

public function approuverRdvAction(Request $request, Booking $booking)
{
    $em = $this->getDoctrine()->getManager();
    $booking->acceptRdv();

    $em->flush();

    $mailer = $this->get('mailer');
    $message = (new \Swift_Message('Votre Rdv est approuvé par le médecin'))
        ->setFrom("medmamtest@gmail.com")
        ->setTo($booking->getPatient()->getUser()->getUsername())
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/confirmation.html.twig',
                array('name' => 'Cher(s)')
            ),
            'text/html'
        );

    $mailer->send($message);

    return $this->render('DoctixMedecinBundle:Medecin:bookingConfirm.html.twig', array(
            'bookings' => $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")
    */
    private $dateRdv;

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

    public function __constructor() {
        $this->validerRdv = false;
    }

    /**
    * The medecin accept the booking
    * @return Booking
    */
    public function acceptRdv()
    {
        $this->validerRdv = true;

        return $this;
    }

    /**
    * The medecin refuse the booking
    * Add this method only if it's possible to refuse a Rdv after accept it
    * @return Booking
    */
    public function refuseRdv() 
    {
        $this->validerRdv = false;

        return $this;
    }

    /**
    * Do the medecin accepted the booking ?
    * @return bool
    */
    public function isValiderRdv()
    {
        return $this->validerRdv;
    }

    // ..................... OTHER FIELDS AND GETTER/SETTER ...............
}