• <legend id='h2wjI'><style id='h2wjI'><dir id='h2wjI'><q id='h2wjI'></q></dir></style></legend>

    <i id='h2wjI'><tr id='h2wjI'><dt id='h2wjI'><q id='h2wjI'><span id='h2wjI'><b id='h2wjI'><form id='h2wjI'><ins id='h2wjI'></ins><ul id='h2wjI'></ul><sub id='h2wjI'></sub></form><legend id='h2wjI'></legend><bdo id='h2wjI'><pre id='h2wjI'><center id='h2wjI'></center></pre></bdo></b><th id='h2wjI'></th></span></q></dt></tr></i><div id='h2wjI'><tfoot id='h2wjI'></tfoot><dl id='h2wjI'><fieldset id='h2wjI'></fieldset></dl></div>

    <small id='h2wjI'></small><noframes id='h2wjI'>

      <bdo id='h2wjI'></bdo><ul id='h2wjI'></ul>

    1. <tfoot id='h2wjI'></tfoot>
      1. Symfony2, Doctrine2 - 按类别和查询显示项目

        Symfony2, Doctrine2 - display items by Category with query(Symfony2, Doctrine2 - 按类别和查询显示项目)
            <tbody id='JLxFy'></tbody>

          <small id='JLxFy'></small><noframes id='JLxFy'>

            <legend id='JLxFy'><style id='JLxFy'><dir id='JLxFy'><q id='JLxFy'></q></dir></style></legend>
            <tfoot id='JLxFy'></tfoot>
              <bdo id='JLxFy'></bdo><ul id='JLxFy'></ul>

                1. <i id='JLxFy'><tr id='JLxFy'><dt id='JLxFy'><q id='JLxFy'><span id='JLxFy'><b id='JLxFy'><form id='JLxFy'><ins id='JLxFy'></ins><ul id='JLxFy'></ul><sub id='JLxFy'></sub></form><legend id='JLxFy'></legend><bdo id='JLxFy'><pre id='JLxFy'><center id='JLxFy'></center></pre></bdo></b><th id='JLxFy'></th></span></q></dt></tr></i><div id='JLxFy'><tfoot id='JLxFy'></tfoot><dl id='JLxFy'><fieldset id='JLxFy'></fieldset></dl></div>
                2. 本文介绍了Symfony2, Doctrine2 - 按类别和查询显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在创建第二个路由,用于在 URL 中按类别显示所有文章.这是我的路线:

                  I'm creating second route for display all articles by category in the URL. There is my route for that:

                  default_blog_category:
                      path:     /category/{slug}/{page}
                      defaults: { _controller: AcmeBlogBundle:Default:indexByCategory, page: 1 }
                  

                  这是我的尝试,按类别获取项目,但它不起作用(我会收到此错误消息:

                  And here is my try, to get items by category, but it does not works (I'll get this error message:

                  [Semantical Error] line 0, col 47 near 'categories =': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
                  

                  ):

                  public function indexByCategoryAction($slug, $page)
                  {
                      $em = $this->getDoctrine()->getManager();
                      $dql = "SELECT a FROM AcmeBlogBundle:Article a WHERE a.categories = :category ORDER BY a.published DESC";
                      $query = $em->createQuery($dql)->setParameter('category', $slug);
                  
                      $paginator = $this->get('knp_paginator');
                      $pagination = $paginator->paginate(
                          $query,
                          $this->get('request')->query->get('page', $page),
                          10
                      );
                  
                      return $this->render('AcmeBlogBundle:Default:index.html.twig', array('pagination' => $pagination));
                  }
                  

                  我不确定,现在如何在查询中使用 WHERE,这也是我的 Article.php 实体:

                  I'm not sure, how to use WHERE in my query for now, here is also my Article.php entity:

                  <?php
                  
                  namespace AcmeBlogBundleEntity;
                  
                  use DoctrineORMMapping as ORM;
                  use SymfonyComponentValidatorConstraints as Assert;
                  use GedmoMappingAnnotation as Gedmo;
                  
                  /**
                   * @ORMEntity(repositoryClass="AcmeBlogBundleEntityArticleRepository")
                   * @ORMTable(name="articles") 
                   */
                  class Article
                  {
                      /**
                       * @ORMId
                       * @ORMColumn(type="integer")
                       * @ORMGeneratedValue(strategy="AUTO")
                       */
                      private $id;              
                  
                      /**
                       * @ORMColumn(type="string", length=200)    
                       * @AssertNotBlank(
                       *      message = "Title cannot be blank"      
                       * )    
                       * @AssertLength(
                       *      min = "3",
                       *      minMessage = "Title is too short"         
                       * )     
                       */     
                      private $title;
                  
                      /**
                       * @GedmoSlug(fields={"title"}, updatable=true, separator="-") 
                       * 
                       * @ORMColumn(name="slug", type="string", length=200, nullable=false, unique=true)
                       */
                      private $slug;
                  
                      /**
                       * @ORMColumn(type="datetime")    
                       * @AssertNotBlank(
                       *      message = "DateTime cannot be blank"
                       * )
                       * @AssertDateTime(
                       *      message = "DateTime is not valid"
                       * )     
                       */
                      protected $published;
                  
                      /**
                       * @ORMManyToMany(targetEntity="Category", inversedBy="articles")
                       * @AssertCount(min = 1, minMessage = "Choose any category") 
                       */
                      private $categories;
                  
                      /**
                       * @ORMOneToMany(targetEntity="Comment", mappedBy="article")
                       * @ORMOrderBy({"published" = "ASC"})     
                       */
                      private $comments;                       
                  
                      /**
                       * @ORMColumn(type="text")
                       * @AssertNotBlank(
                       *      message = "Perex cannot be blank"      
                       * )    
                       * @AssertLength(
                       *      min = "5",
                       *      minMessage = "Perex is too short"         
                       * )     
                       */
                      private $perex;
                  
                      /**
                       * @ORMColumn(type="text")
                       * @AssertNotBlank(
                       *      message = "Content cannot be blank"      
                       * )    
                       * @AssertLength(
                       *      min = "10",
                       *      minMessage = "Content must have min. 10 characters"         
                       * )     
                       */
                      private $content;
                  
                      /**
                       * @ORMColumn(type="string", length=200)     
                       */     
                      private $description;
                  
                      /**
                       * @ORMColumn(type="string", length=200)     
                       */     
                      private $keywords;
                  
                      /**
                       * Constructor
                       */
                      public function __construct()
                      {
                          $this->categories = new DoctrineCommonCollectionsArrayCollection();
                          $this->comments = new DoctrineCommonCollectionsArrayCollection();
                      }
                  
                      /**
                       * Get id
                       *
                       * @return integer 
                       */
                      public function getId()
                      {
                          return $this->id;
                      }
                  
                      /**
                       * Set title
                       *
                       * @param string $title
                       * @return Article
                       */
                      public function setTitle($title)
                      {
                          $this->title = $title;
                  
                          return $this;
                      }
                  
                      /**
                       * Get title
                       *
                       * @return string 
                       */
                      public function getTitle()
                      {
                          return $this->title;
                      }
                  
                      /**
                       * Set slug
                       *
                       * @param string $slug
                       * @return Article
                       */
                      public function setSlug($slug)
                      {
                          $this->slug = $slug;
                  
                          return $this;
                      }
                  
                      /**
                       * Get slug
                       *
                       * @return string 
                       */
                      public function getSlug()
                      {
                          return $this->slug;
                      }
                  
                      /**
                       * Set content
                       *
                       * @param string $content
                       * @return Article
                       */
                      public function setContent($content)
                      {
                          $this->content = $content;
                  
                          return $this;
                      }
                  
                      /**
                       * Get content
                       *
                       * @return string 
                       */
                      public function getContent()
                      {
                          return $this->content;
                      }
                  
                      /**
                       * Add categories
                       *
                       * @param AcmeBlogBundleEntityCategory $categories
                       * @return Article
                       */
                      public function addCategory(AcmeBlogBundleEntityCategory $categories)
                      {
                          //$this->categories[] = $categories;
                  
                         //return $this;
                         if (!$this->categories->contains($categories)) {
                              $this->categories->add($categories);
                              $categories->addArticle($this);
                          }
                      }
                  
                      /**
                       * Remove categories
                       *
                       * @param AcmeBlogBundleEntityCategory $categories
                       */
                      public function removeCategory(AcmeBlogBundleEntityCategory $categories)
                      {
                          $this->categories->removeElement($categories);
                      }
                  
                      /**
                       * Get categories
                       *
                       * @return DoctrineCommonCollectionsCollection 
                       */
                      public function getCategories()
                      {
                          return $this->categories;
                      }
                  
                      /**
                       * Add comments
                       *
                       * @param AcmeBlogBundleEntityComment $comments
                       * @return Article
                       */
                      public function addComment(AcmeBlogBundleEntityComment $comments)
                      {
                          $this->comments[] = $comments;
                  
                          return $this;
                      }
                  
                      /**
                       * Remove comments
                       *
                       * @param AcmeBlogBundleEntityComment $comments
                       */
                      public function removeComment(AcmeBlogBundleEntityComment $comments)
                      {
                          $this->comments->removeElement($comments);
                      }
                  
                      /**
                       * Get comments
                       *
                       * @return DoctrineCommonCollectionsCollection 
                       */
                      public function getComments()
                      {
                          return $this->comments;
                      }
                  
                      /**
                       * Set published
                       *
                       * @param DateTime $published
                       * @return Article
                       */
                      public function setPublished($published)
                      {
                          $this->published = $published;
                  
                          return $this;
                      }
                  
                      /**
                       * Get published
                       *
                       * @return DateTime 
                       */
                      public function getPublished()
                      {
                          return $this->published;
                      }
                  
                      /**
                       * Set description
                       *
                       * @param string $description
                       * @return Article
                       */
                      public function setDescription($description)
                      {
                          $this->description = $description;
                  
                          return $this;
                      }
                  
                      /**
                       * Get description
                       *
                       * @return string 
                       */
                      public function getDescription()
                      {
                          return $this->description;
                      }
                  
                      /**
                       * Set keywords
                       *
                       * @param string $keywords
                       * @return Article
                       */
                      public function setKeywords($keywords)
                      {
                          $this->keywords = $keywords;
                  
                          return $this;
                      }
                  
                      /**
                       * Get keywords
                       *
                       * @return string 
                       */
                      public function getKeywords()
                      {
                          return $this->keywords;
                      }
                  
                      /**
                       * Set perex
                       *
                       * @param string $perex
                       * @return Article
                       */
                      public function setPerex($perex)
                      {
                          $this->perex = $perex;
                  
                          return $this;
                      }
                  
                      /**
                       * Get perex
                       *
                       * @return string 
                       */
                      public function getPerex()
                      {
                          return $this->perex;
                      }
                  }
                  

                  有什么想法吗?

                  推荐答案

                  您必须加入类别.此代码应放置在 ArticleRepository 中.

                  You have to join the categories. This code should be placed in the ArticleRepository.

                  $qb = $this->createQueryBuilder('a');
                  $qb->add('select', 'a');
                  $qb->leftJoin('a.category', 'c');
                  $qb->where('c.name LIKE :category'); /* i have guessed a.name */
                  $qb->setParameter('category', $slug);
                  $qb->getQuery()->getResult();
                  

                  参见 doctrine 查询构建器 和 symfony 自定义存储库类 教程.

                  这篇关于Symfony2, Doctrine2 - 按类别和查询显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)
                    <bdo id='6awm6'></bdo><ul id='6awm6'></ul>
                    <i id='6awm6'><tr id='6awm6'><dt id='6awm6'><q id='6awm6'><span id='6awm6'><b id='6awm6'><form id='6awm6'><ins id='6awm6'></ins><ul id='6awm6'></ul><sub id='6awm6'></sub></form><legend id='6awm6'></legend><bdo id='6awm6'><pre id='6awm6'><center id='6awm6'></center></pre></bdo></b><th id='6awm6'></th></span></q></dt></tr></i><div id='6awm6'><tfoot id='6awm6'></tfoot><dl id='6awm6'><fieldset id='6awm6'></fieldset></dl></div>
                  • <small id='6awm6'></small><noframes id='6awm6'>

                          <tfoot id='6awm6'></tfoot>

                            <legend id='6awm6'><style id='6awm6'><dir id='6awm6'><q id='6awm6'></q></dir></style></legend>
                              <tbody id='6awm6'></tbody>