• <small id='LCFP7'></small><noframes id='LCFP7'>

    1. <legend id='LCFP7'><style id='LCFP7'><dir id='LCFP7'><q id='LCFP7'></q></dir></style></legend>
      • <bdo id='LCFP7'></bdo><ul id='LCFP7'></ul>

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

        <tfoot id='LCFP7'></tfoot>

        Doctrine2 关联映射与条件

        时间:2023-08-19
        <i id='78eT0'><tr id='78eT0'><dt id='78eT0'><q id='78eT0'><span id='78eT0'><b id='78eT0'><form id='78eT0'><ins id='78eT0'></ins><ul id='78eT0'></ul><sub id='78eT0'></sub></form><legend id='78eT0'></legend><bdo id='78eT0'><pre id='78eT0'><center id='78eT0'></center></pre></bdo></b><th id='78eT0'></th></span></q></dt></tr></i><div id='78eT0'><tfoot id='78eT0'></tfoot><dl id='78eT0'><fieldset id='78eT0'></fieldset></dl></div>
          <tbody id='78eT0'></tbody>
            <legend id='78eT0'><style id='78eT0'><dir id='78eT0'><q id='78eT0'></q></dir></style></legend>
                <bdo id='78eT0'></bdo><ul id='78eT0'></ul>
              • <tfoot id='78eT0'></tfoot>
                • <small id='78eT0'></small><noframes id='78eT0'>

                  本文介绍了Doctrine2 关联映射与条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以在 Doctrine 2.4 中对条件进行关联映射?我有实体文章和评论.评论需要管理员批准.评论的批准状态存储在布尔字段已批准"中.

                  is it possible to have association mapping with conditions in Doctrine 2.4? I have entities Article and Comment. Comments needs to be approved by admin. Approval status of comment is stored in boolean field "approved.

                  现在我有@OneToMany 关联映射到实体文章中的评论.它映射了所有的评论.但我只想映射已批准的评论.

                  Now I have @OneToMany association mapping to comments in entity Article. It maps all the comments. But I would like to map only approved comments.

                  类似的东西

                  @ORMOneToMany(targetEntity="Comment", where="approved=true", mappedBy="article")
                  

                  会很有帮助.不幸的是,AFAIK 在映射中没有 where 条件这样的东西,所以我试图用继承来解决我的问题 - 我创建了类 Comment 的两个子类.现在我有 ApprovedComment 和 NotApprovedComment 和 SINGLE_TABLE 继承映射.

                  would be very helpful. Unfortunately AFAIK there is no such thing as where condition in mapping, so I tried to solve my problem with inheritance - I created two subclasses of class Comment. Now I have ApprovedComment and NotApprovedComment and SINGLE_TABLE inheritance mapping.

                   @ORMInheritanceType("SINGLE_TABLE")
                   @ORMDiscriminatorColumn(name="approved", type="integer")
                   @ORMDiscriminatorMap({1 = "ApprovedComment", 0 = "NotApprovedComment"})
                  

                  问题是,由于已批准"列是鉴别符,我不能再将其用作实体评论中的字段.

                  Problem is, since "approved" column is discriminator, I cannot use it as field in entity Comment anymore.

                  推荐答案

                  您可以使用 Criteria API 来过滤 集合:

                  You can use the Criteria API to filter the collection:

                  <?php
                  
                  use DoctrineCommonCollectionsCriteria;
                  
                  class Article
                  {
                  
                      /**
                       * @ORMOneToMany(targetEntity="Comment", mappedBy="article")
                       */
                      protected $comments;
                  
                      public function getComments($showPending = false)
                      {
                          $criteria = Criteria::create();
                          if ($showPending !== true) {
                              $criteria->where(Criteria::expr()->eq('approved', true));
                          }
                          return $this->comments->matching($criteria);
                      }
                  
                  }
                  

                  这特别好,因为 Doctrine 足够聪明,只有在集合尚未加载的情况下才转到数据库.

                  This is especially nice, because Doctrine is smart enough to only go to the database if the collection hasn't already been loaded.

                  这篇关于Doctrine2 关联映射与条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:学说的多对多自参照和互惠 下一篇:Doctrine2 - “类"不是有效的实体或映射的超类

                  相关文章

                  <tfoot id='L3N1W'></tfoot>
                    <bdo id='L3N1W'></bdo><ul id='L3N1W'></ul>

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

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

                    <legend id='L3N1W'><style id='L3N1W'><dir id='L3N1W'><q id='L3N1W'></q></dir></style></legend>