首页 > 解决方案 > 在 symfony 3 中调用 null 上的成员函数 getId()

问题描述

我正在尝试检索请求中传递的属性的值,我尝试过,我做不到,当我对属性进行 var_dump 时,它返回一个 NULL。

首先我创建了一个表单来输入评论,当我们点击验证时,这个页面将不得不带你回到我有每个医生的评论列表的页面,但我只收到这个错误,告诉我“在 null 上调用成员函数 getId()”,因为我无法恢复医生的值

创建评论的控制器

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();
}

commentsAction 和 createcomment 的路由

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

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

View of comments动作

{% 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>

标签: symfonyrequest

解决方案


尽管问题仍然缺乏您如何准确访问此路线的信息 - 即为什么您假设药物 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);
    ...

推荐阅读