Zenzs
Zenzs

Reputation: 138

order not working with sortWhitelist

Using CakePHP 3.5.3

Hi,

The below code works works as it should and displays the result set and orders it by the due_date asc.

public $paginate = [        
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];   

public function index()
{        
    $session = $this->request->session();

    // Declare client id 1
    $cidOne = null;
    $cidOne = $session->read('Cid.one');

    // NOTE* DON'T USE ORDER HERE BECAUSE THE SORTS WILL NOT WORK
    $query = $this->Activities->find('all')
    ->where(['cid_1' => $cidOne])
    ->andWhere(['status' => 1]);

    // Send the query to the view.
    $this->set('activities', $this->paginate($query));
}

But when I add sortWhitelist as below the initial page load is not sorted by the due_date and no sort arrow is displayed.

public $paginate = [
    'sortWhitelist' => [
       'due_date', 'related_to', 'subject', 'post_code', 'type', 'priority'
    ],
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];

Thanks for any help. Z.

Upvotes: 0

Views: 366

Answers (1)

arilia
arilia

Reputation: 9398

There must be consistency between the sortWhitelist array, the order array and the paginator link

so if your field is Activities.due_date your code becomes:

public $paginate = [
    'sortWhitelist' => [
       'Activities.due_date', 'related_to', 'subject', 'post_code', 'type', 'priority'
    ],
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];

and in your view

<?= $this->Paginator->sort('Activities.due_date', __('Due Date')) ?>

If there is no ambiguity in the fields names you can omit the Model name and simply use due_date as column name

Upvotes: 1

Related Questions