• <bdo id='oWkN9'></bdo><ul id='oWkN9'></ul>

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

    <tfoot id='oWkN9'></tfoot>

      1. <i id='oWkN9'><tr id='oWkN9'><dt id='oWkN9'><q id='oWkN9'><span id='oWkN9'><b id='oWkN9'><form id='oWkN9'><ins id='oWkN9'></ins><ul id='oWkN9'></ul><sub id='oWkN9'></sub></form><legend id='oWkN9'></legend><bdo id='oWkN9'><pre id='oWkN9'><center id='oWkN9'></center></pre></bdo></b><th id='oWkN9'></th></span></q></dt></tr></i><div id='oWkN9'><tfoot id='oWkN9'></tfoot><dl id='oWkN9'><fieldset id='oWkN9'></fieldset></dl></div>
      2. <legend id='oWkN9'><style id='oWkN9'><dir id='oWkN9'><q id='oWkN9'></q></dir></style></legend>
      3. 如何在 Symfony 2 和 Doctrine 中过滤实体对象内的数据

        时间:2023-08-19

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

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

              <tbody id='PED9x'></tbody>
            • <tfoot id='PED9x'></tfoot>

              • <bdo id='PED9x'></bdo><ul id='PED9x'></ul>
                1. <small id='PED9x'></small><noframes id='PED9x'>

                  本文介绍了如何在 Symfony 2 和 Doctrine 中过滤实体对象内的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个实体:ProductFeature.Product 有许多其他 Features(一对多的关系).每个Feature 都有一个名称和一个重要的状态(如果特性重要则为真,否则为假).我想在 TWIG 中使用我的产品的所有重要功能.

                  I have two entities: Product and Feature. Product has many other Features (relation one to many). Every Feature has a name and an important status (true if feature is important, false if not). I want to get in TWIG all important features for my product.

                  下面的解决方案非常难看:

                  Solution below is very ugly:

                  Product: {{ product.name }}
                  Important features:
                  {% for feature in product.features %}
                      {% if feature.important == true %}
                          - {{ feature.name }}
                      {% endif %}
                  {% endfor %}
                  

                  所以我想得到:

                  Product: {{ product.name }}
                  Important features:
                  {% for feature in product.importantFeatures %}
                       - {{ feature.name }}
                  {% endfor %}
                  

                  我必须过滤实体对象中的数据,但如何过滤?

                  I must filter data in entity object, but how?

                  // MyBundle/Entity/Vehicle.php
                  class Product {
                      protected $features; // (oneToMany)
                      // ...
                      protected getFeatures() { // default method
                          return $this->features;
                      }
                      protected getImportantFeatures() { // my custom method
                          // ? what next ?
                      }
                  }
                  
                  // MyBundle/Entity/Feature.php
                  class Feature {
                      protected $name;      // (string)
                      protected $important; // (boolean)
                      // ...
                  }
                  

                  推荐答案

                  您可以使用 Criteria 类过滤掉相关特征的Arraycollection

                  You can use Criteria class to filter out the Arraycollection of related features

                  class Product {
                      protected $features; // (oneToMany)
                      // ...
                      protected getFeatures() { // default method
                          return $this->features;
                      }
                      protected getImportantFeatures() { // my custom method
                          $criteria = DoctrineCommonCollectionsCriteria::create()
                                      ->where(DoctrineCommonCollectionsCriteria::expr()->eq("important", true));
                       return $this->features->matching($criteria);
                      }
                  }
                  

                  在树枝中

                  Product: {{ product.name }}
                  Important features:
                  {% for feature in product.getImportantFeatures() %}
                       - {{ feature.name }}
                  {% endfor %}
                  

                  这篇关于如何在 Symfony 2 和 Doctrine 中过滤实体对象内的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Symfony2 Doctrine PDO MySQL 连接与 LOAD DATA LOCAL INFILE 下一篇:什么级联“刷新"在教义 2 中是什么意思?

                  相关文章

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

                  1. <tfoot id='j8WBP'></tfoot>
                      <bdo id='j8WBP'></bdo><ul id='j8WBP'></ul>

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

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