Cakephp 3 sort By DESC两个出现故障

时间:2018-06-25 18:12:53

标签: sorting collections cakephp-3.x

对不起,更多详细信息。我有两个表: 表1。

**faculty_salaries**
id
personnel_profile_id
faculty_position_id
faculty_salary

表2。

**personnel_profiles**
id
azinstitution_id
fy_year
title
faculty_turnover_rate
staff_turnover_rate

页面的我的控制器是这个。

 public function asuSalaries()
    {
        $associated = ['PersonnelProfiles', 'StaffPositions', 'PersonnelProfiles.Azinstitutions'];
        $staffSalaries = $this->StaffSalaries->find('all')->contain($associated)->where(['PersonnelProfiles.azinstitution_id' => 1])->sortBy('PersonnelProfiles.fy_year', SORT_DESC);
        $collection = new Collection($staffSalaries);
        $salaryYears = $collection->match(['staff_position_id' => 1]);
        $asuAdministrators = $collection->match(['staff_position_id' => 1])->extract('staff_salary');
        $asuStaffs = $collection->match(['staff_position_id' => 2])->extract('staff_salary');
        $this->set(compact('staffSalaries','salaryYears', 'asuAdministrators', 'asuStaffs'));
    }

视图如下:

<thead class="thead-inverse asu-table-color">
       <tr>
          <th scope="col">Position</th>
          <!--- loop to get all the year titles for the columns -->
                <?php foreach ($salaryYears as $fyYear): ?>
                    <th><?= h($fyYear->personnel_profile->title)  ?></th> 
                <?php endforeach; ?>
       </tr>
  </thead>

不添加排序,它将返回(例如,salaryYears集合)

0 FY 2017
1 FY 2016
2 FY 2015
3 FY 2014
4 FY 2013
5 FY 2012

在我看来,我想颠倒该顺序,因此我添加了sortBy来颠倒它,但它返回:

salaryYears (array)
4 FY 2013
5 FY 2012
3 FY 2014
2 FY 2015
1 FY 2016
0 FY 2017

不确定为什么会失败ID#4和5

1 个答案:

答案 0 :(得分:0)

使用表中的数据(例如“ year”列)而不是ID进行排序要好得多。我经常遇到ID乱序的问题(旧版本位于新版本下方)。