【问题标题】:How to filter by tag in Silverstripe, when using tagfield module?使用标签字段模块时,如何在 Silverstripe 中按标签过滤?
【发布时间】:2020-07-23 07:42:20
【问题描述】:

我在 Silverstripe 中有以下页面类型,其中包含许多 CategoryTag。 CategoryTag 是使用 silverstripe-tagfield 模块创建的数据对象。

class ArticlePage extends Page {
 ...
  private static $many_many = array(
            'CategoryTags' => 'CategoryTag'
        );
...

我想将具有匹配 CategoryTags 的所有其他 ArticlePages 返回到当前页面。我知道我可以返回所有其他 ArticlePages 并过滤它们,例如:

public function getRelatedArticles() {
            $relatedArticles = ArticlePage::get()->filter(SOME FILTER);
             return ($relatedArticles);
         }

但我不确定按当前页面的 CategoryTags 过滤的语法。我将如何调整上述功能来实现这一目标?提前致谢。

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    费了好大劲,终于找到了解决办法:

     public function getRelatedArticles(){
        return ArticlePage::get()->filter('CategoryTags.ID', $this->CategoryTags()->column('ID'))->exclude('ID', $this->ID);
     }
    

    希望这可以为将来节省一些时间:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2012-11-28
      • 2017-05-25
      相关资源
      最近更新 更多