在symfony 3中的null上调用成员函数getId()

时间:2018-11-28 14:34:00

标签: symfony request

我正在尝试检索在请求中传递的属性的值,我尝试过,但是不能,当我在该属性上进行var_dump时它返回NULL。

首先,我创建了一个表单以输入评论,然后单击“验证”,此页面将不得不带您回到我拥有每个医生的评论列表的页面,但是我只会收到此错误,告诉我“在null上调用成员函数getId()”,因为我无法恢复doctor的值

创建评论的控制器

public function commentCreateAction(Request $request, Booking $bookings)
{

    $em = $this->getDoctrine()->getEntityManager();

    //  $medecin = $booking->getMedecin();
    $patient = $bookings->getPatient();

    $repoMedecin = $em->getRepository('DoctixMedecinBundle:Medecin');

    $medecin = $repoMedecin->findOneBy(array(
        'id' => $request->query->get("medecin")
    ));

    $bookings = $em->getRepository("DoctixFrontBundle:Booking")->findBy(array(
        "patient" => $patient
    ));

    $comments = new Comment();
    $comments->setMedecin($medecin);
    $comments->setPatient($patient);

    $form = $this->createForm('Doctix\PatientBundle\Form\CommentType', $comments);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($comments);
        $em->flush();

        return $this->render('DoctixPatientBundle:Patient:comments.html.twig', array(
            'id' => $comments->getMedecin()->getId(),
            'comments' => $comments, 
            'bookings' => $bookings
        ));

    }

    return $this->render('DoctixPatientBundle:Patient:create.html.twig', array(
        'comment' => $comment,
        'form'    => $form->createView()
    ));
}

评论列表中的控制器

public function commentsAction(Request $request){

    $em  = $this->getDoctrine()->getManager();
    $repoMedecin = $em->getRepository('DoctixMedecinBundle:Medecin');

    $medecin = $repoMedecin->findOneBy(array(
        'id' => $request->query->get("medecin")
    )); 

    $patient = $em->getRepository("DoctixPatientBundle:Patient")->findOneBy(array(
        'user' => $this->getUser(),
    ));

    $bookings = $em->getRepository("DoctixFrontBundle:Booking")->findBy(array(
        "patient" => $patient
    ));


    ***
    $comments = $em->getRepository("DoctixPatientBundle:Comment")
        ->getCommentsForDoc($medecin->getId());
    ***

    return $this->render('DoctixPatientBundle:Patient:comments.html.twig', array(
        'comments' => $comments,
        'bookings' => $bookings
    ));
}

我的错误是在此控制器中。

要征询医生的意见,我的存储库中有一个功能

存储库

public function getCommentsForDoc($docId, $approved = true)
{
    $qb = $this->createQueryBuilder('c')
        ->select('c')
        ->where('c.medecin = :medecin_id')
        ->addOrderBy('c.created')
        ->setParameter('medecin_id', $docId);

    if (false === is_null($approved))
        $qb->andWhere('c.approved = :approved')->setParameter('approved', $approved);

    return $qb->getQuery()->getResult();
}

评论路由动作和创建评论

patient_comments:
    path:  /patient/medecin/comments
    defaults: { _controller: DoctixPatientBundle:Patient:comments}

patient_comments_create:
    path:  /patient/medecin/{id}/create
    defaults: { _controller: DoctixPatientBundle:Patient:commentCreate}

评论视图操作

{% for booking in bookings %}

    <div class="list_general" id="liste">
    <a href="#0" class="wish_bt"></a>
    <ul>
    <li>
    <figure>
        <img src="{{ vich_uploader_asset(booking.medecin.media, 'imageFile') }}"
            alt="{{ booking.medecin.media.imagename }}">
    </figure>

    <h4>
        Dr. {{ booking.medecin.user.prenom|capitalize }} {{ booking.medecin.user.nom|upper }} 
        <i class="pending">Pending</i>
    </h4>

    <a href="{{ path('patient_comments_create', {'id': booking.id}) }}?medecin={{ booking.medecin.id }}">
        Ajouter un Commentaire
    </a>

    <header>
        {% for comment in comments %}

        {% if comment is defined %}

        <label>Nom</label>
        <input type="text" class="form-control" readonly
            value=" {{ comment.patient.user.nom }} ">

        <label>Nom</label>
        <input type="text" class="form-control" readonly
            value=" {{ comment.patient.user.prenom }} ">

        <p> a commenté </p> 
        <p>
            <time datetime="{{ comment.created|date('c') }}"> 
                {{ comment.created|date('l, F j, Y') }}
            </time> 
        </p>
    </header>

    <label>My Comments</label>
    <input type="text" class="form-control" readonly
        value=" {{ comment.comment }} ">
    {% else %}
        <p> Il n'y a pas encore de commentaires à propos de ce médecin. Soyez le premier à commenter....</p>
    {% endif %}

    {% endfor %} 

{% endfor %}

我的布局中有“反馈”标签和我的路线的地方

   <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Mon profil">
                    <a class="nav-link link-white" href="{{ path('patient_comments')}}">
                        <i class="fa fa-fw fa-comments"></i>
                        <span class="nav-link-text">FeedBack</span>
                    </a>
                </li>

1 个答案:

答案 0 :(得分:0)

尽管问题仍然缺少您如何精确地访问此路由的信息-即为什么您假设医学ID将在请求参数中传递-对我来说,您的路由定义似乎缺少ID:

patient_comments:
    path:  /patient/medecin/{id}/comments
    defaults: { _controller: DoctixPatientBundle:Patient:comments}

public function commentsAction(Request $request, $id)
{
    $em  = $this->getDoctrine()->getManager();
    $repoMedecin = $em->getRepository('DoctixMedecinBundle:Medecin');

    $medecin = $repoMedecin->find($id);
    ...