好的,更多细节对不起。我有两张桌子:
表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
不知道为什么它会失败