1

Okay more details sorry. I have two tables: Table 1.

**faculty_salaries**
id
personnel_profile_id
faculty_position_id
faculty_salary

Table 2.

**personnel_profiles**
id
azinstitution_id
fy_year
title
faculty_turnover_rate
staff_turnover_rate

my controller for the page is this.

 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'));
    }

The view looks like this:

<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>

Without the sort added it would returns(for the salaryYears collection, just as an example)

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

In my view I wanted to reverse that order so I then added the sortBy to reverse the it but It returns:

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

Not sure why it flops id #4 and 5

Ken
  • 41
  • 9
  • Again, after `extract('personnel_profile.title')` there basically will be no `id` field anymore, once unwrapped by the next method, the collections source will just be an array of strings, so you cannot sort it that way. I can't tell if you're doing something in your view (sorting again?) that might mess things up, or if you're talking about the code in the controller when you say you want to reverse the order in your view, in any case, you can't sort as shown after extracting a single field. ps, it's always a good idea to notify people of changes, I just saw it by chance. – ndm Jun 26 '18 at 22:25
  • my view is just a foreach loop so it is just returning the array as is. Looks like extract might not be the best method. – Ken Jun 30 '18 at 18:51
  • So I've updated the question. I have removed the extract and pulled the title out in the view. I moved the sortBy to the original call and I'm sorting by the year not title, but i see no change. It flops the 2013 and 2012. It is correct in ASC order. – Ken Jun 30 '18 at 19:29

1 Answers1

0

It's much better to sort using the data in the table such as the "year" column rather than the ID. I regularly have issues with IDs being out of order (older sitting below newer).

Quasi635
  • 65
  • 2
  • 9