• <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 关联映射与条件

        Doctrine2 association mapping with conditions(Doctrine2 关联映射与条件)
        <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 关联映射与条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 函数)

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

                      • <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>

                              <tbody id='L3N1W'></tbody>