【问题标题】:sort has_many by has_one relation in silverstripe在 silverstripe 中按 has_one 关系对 has_many 进行排序
【发布时间】:2013-11-24 23:06:01
【问题描述】:

我在 Silverstripe 3.1 中有三个数据对象:一个问题、一个投票和一个投票者。问题有很多投票;投票有一个投票者和一个问题。在我的 Issue_show 页面上,我想显示所有问题的投票,按投票者姓名排序。

Issue 中的函数如下所示:

public function MyVotes() {
     return $this->Votes();
}

但我不知道如何访问选民姓名以对其进行排序。大概是这样的

public function MyVotes() {
    return $this->Votes()->sort('Voter.Name');
} 

但这会引发错误。我错过了哪一步?

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    对于 has_one 关系,您需要将 ID 后缀添加到字段名。此外,不幸的是,DataList->sort() 中的关系转换仅适用于数组。

    public function MyVotes() {
    return $this->Votes()->sort(array('VoterID.Name'=>'ASC'));
    }
    

    【讨论】:

      【解决方案2】:

      您还可以像这样在模板中处理排序:

      <% loop Votes.Sort('VoterID.Name') %>
          ...
      

      这还没有经过测试,但很确定应该可以工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        相关资源
        最近更新 更多