Symfony不可能访问null变量上的属性(“ nameArticle”)

时间:2018-06-23 21:58:16

标签: php sql symfony

我创建了一个博客,并且有一个评论系统。我有一点问题要发表评论。当存在评论时,文章将正确显示其评论,但是当文章没有评论时,问题将显示给我这样的错误

  

无法访问null变量上的属性(“ nameArticle”)。

我试图添加一个 if ,但是它不起作用,不会向我显示该文章!

我的控制器:

/**
 * @Route("/{slug}")
 * @param string $slug
 * @param Request $request
 * @return Response
 * @throws \Exception
 */
 public function detail(Request $request, string $slug): Response {

     $article = $this->getDoctrine()
         ->getRepository(Articles::class)
         ->findOneWithCategorySlug($slug);

     $info = $this->getDoctrine()
         ->getRepository(InfoMore::class)
         ->findByInfo(2);

     // $comments = $this->getDoctrine()->getRepository(Comments::class)->findAll();

     $comment = new Comments();
     $form = $this->createForm(CommentsType::class, $comment);
     $form->handleRequest($request);

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

         $comments = $form->getData();
         $manager = $this->getDoctrine()->getManager();
         $user = $this->getUser();
         $comments->setUserComments($user);
         $comments->setArticleComments($article);
         $manager->persist($comments);
         $manager->flush();
         $this->addFlash('notice', 'Le commentaire a bien été ajouté ');
         return $this->redirectToRoute('app_home_detail', [
             'slug' => $slug
         ]);
     }

     return $this->render('front/detail_article.html.twig', [
         'title' => $article,
         'article' => $article,
         'info' => $info,
         'description' => '',
         'keywords' => '',
         'addComments' => $form->createView()
    ]);
}

我的观点:

{% extends 'base.html.twig' %}

{% set active = '' %}
{% set active2 = '' %}
{% set active3 = '' %}
{% set active4 = '' %}
{% set active5 = '' %}
{% set active6 = '' %}

{% if title %}
{% set title = article.nameArticle %}
{% else %}
    {% set title = 'Erreur article non trouver !' %}
{% endif %}

{% block body %}
<style>
    /* Style the Image Used to Trigger the Modal */
    #myImg {
        border-radius: 5px;
        cursor: pointer;
        transition: 0.3s;
    }

    #myImg:hover {opacity: 0.7;}

    /* The Modal (background) */
    .modal {
        display: none; /* Hidden by default */
        position: fixed; /* Stay in place */
        z-index: 1; /* Sit on top */
        padding-top: 100px; /* Location of the box */
        left: 0;
        top: 0;
        width: 100%; /* Full width */
        height: 100%; /* Full height */
        overflow: auto; /* Enable scroll if needed */
        background-color: rgb(0,0,0); /* Fallback color */
        background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
    }

    /* Modal Content (Image) */
    .modal-content {
        margin: auto;
        display: block;
        width: 100%;
        max-width: 1300px;
    }

    /* Caption of Modal Image (Image Text) - Same Width as the Image */
    #caption {
        margin: auto;
        display: block;
        width: 80%;
        max-width: 700px;
        text-align: center;
        color: #ccc;
        padding: 10px 0;
        height: 150px;
    }

    /* Add Animation - Zoom in the Modal */
    .modal-content, #caption {
        animation-name: zoom;
        animation-duration: 0.6s;
    }

    @keyframes zoom {
        from {transform:scale(0)}
        to {transform:scale(1)}
    }

    /* The Close Button */
    .close {
        position: absolute;
        top: 15px;
        right: 35px;
        color: #f1f1f1;
        font-size: 40px;
        font-weight: bold;
        transition: 0.3s;
    }

    .close:hover,
    .close:focus {
        color: #bbb;
        text-decoration: none;
        cursor: pointer;
    }

    /* 100% Image Width on Smaller Screens */
    @media only screen and (max-width: 700px){
        .modal-content {
            width: 100%;
        }
    }
</style>
        <div class="container">
            <div class="row">
                <div class="col-md-12 col-md-offset-2 col-sm-12 col-sm-offset-0 col-xs-12 col-xs-offset-0 text-center fh5co-table">
                    <div class="fh5co-intro fh5co-table-cell">

                      <h1 class="text-center title-article h1 animate-box">{{ article.nameArticle }}</h1>

                    </div>
                </div>
            </div>
        </div>


    <main role="main" class="container">


    <article>

        <div class="heading-section text-center">
            <h2>Article publié le : {{ article.createdAt | date('d/m/Y à H:i') }} dans {{ article.category }}</h2>
        </div>


            {{ article.introduction | raw }}


        <div class="col-md-10 col-md-offset-1">
            <figure class="text-center" title="Cliquer sur l'images pour la voir en grand">
               <img id="myImg" height="500" width="500" src="{{ asset('images/articles/'~article.imageName) }}" class="img-fluid" alt="{{ article.imageName }}">
            </figure>
        </div>
        <!-- The Modal -->
        <div id="myModal" class="modal">
            <!-- The Close Button -->
            <span class="close">&times;</span>
            <!-- Modal Content (The Image) -->
            <img class="modal-content" id="img01">
            <!-- Modal Caption (Image Text) -->
            <div id="caption"></div>
        </div>

            {{ article.articleContent |raw }}

    </article>

   <div class="espace60"></div>

{% for comment in article.comments %}
    <p>{{ comment.commentsContent }} de la part {{ comment.userComments }}</p>
{% endfor %}


   <div class="espace60"></div>
<hr>
 {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}

     {% for message in app.flashes('notice') %}
         <p>
         <div class="alert alert-success alert-dismissible fade show" role="alert">
             {{ message }}
             <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                 <span aria-hidden="true">&times;</span>
             </button>
         </div>
         </p>
     {% endfor %}

   {{ form_start(addComments) }}
        <div class="col-md-12 animate-box">
            <div class="form-group">
                    {{ form_widget(addComments) }}
                    {{ form_errors(addComments) }}
                <input type="submit" class="btn btn-primary" value="Ajouter un commentaire">
            </div>
        </div>
   {{ form_end(addComments) }}
        {% endif %}
    </main>

    <script>
        // Get the modal
        var modal = document.getElementById('myModal');

        // Get the image and insert it inside the modal - use its "alt" text as a caption
        var img = document.getElementById('myImg');
        var modalImg = document.getElementById("img01");
        var captionText = document.getElementById("caption");
        img.onclick = function(){
            modal.style.display = "block";
            modalImg.src = this.src;
            captionText.innerHTML = this.alt;
        }

        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];

        // When the user clicks on <span> (x), close the modal
        span.onclick = function() {
            modal.style.display = "none";
        }
    </script>

{% endblock %}

和我的存储库:

 /**
  * @param string $slug
  * @return Articles|null
  * @throws \Exception
  */
  public function findOneWithCategorySlug(string $slug): ?Articles {

      $query = $this->createQueryBuilder('a')
          ->join('a.category', 'c')
          ->addSelect('c')
          ->where('a.slug = :slug')
          ->setParameter(':slug', $slug)
          ->innerJoin('a.comments', 'com')
          ->getQuery()
      ;

      try {
          return $query->getOneOrNullResult();
      } catch (\Exception $e) {
          throw new \Exception('Problème dans ArticlesRepository::findOneWithCategory' . $e->getMessage() . var_dump($e));
      }

  }

1 个答案:

答案 0 :(得分:1)

那很正常,在查询中,您使用 innerJoin

  

INNER JOIN关键字选择两个表中具有匹配值的记录。

因此,您的查询将只选择在 Articles Comments 表中具有匹配值的记录。如果没有评论,则不会选择找到“文章”的记录。

如果您想摆脱

  

无法访问null变量上的属性(“ nameArticle”)。

只需使用不为空修复树枝,您就可以添加 defined 来执行验证:

{% if article is defined and article is not null %}
    {% set title = article.nameArticle %}
{% else %}
    {% set title = 'Erreur article non trouver !' %}
{% endif %}

如果即使他们没有评论也要显示文章,请使用 leftJoin 代替 innerJoin

/**
 * @param string $slug
 * @return Articles|null
 * @throws \Exception
 */
public function findOneWithCategorySlug(string $slug): ?Articles {

    $query = $this->createQueryBuilder('a')
        ->join('a.category', 'c')
        ->addSelect('c')
        ->where('a.slug = :slug')
        ->setParameter(':slug', $slug)
        ->leftJoin('a.comments', 'com')
        ->getQuery()
    ;

    try {
        return $query->getOneOrNullResult();
    } catch (\Exception $e) {
        throw new \Exception('Problème dans ArticlesRepository::findOneWithCategory' . $e->getMessage() . var_dump($e));
    }

}