0
votes

with CakePHP I want to use an array with the Paginator component. I'm using the Datasources plugin and I have created the Fake model:

<?php

/**
 * A Fake model.
 */
class Fake extends AppModel {
    public $useDbConfig = 'arraySource';

    public $records = array(
        array('id' => 1, 'name' => 'Alfa', 'height' => 300, 'width' => 300),
        array('id' => 2, 'name' => 'Beta', 'height' => 200, 'width' => 100),
        array('id' => 3, 'name' => 'Gamma', 'height' => 450, 'width' => 200),
        array('id' => 4, 'name' => 'Omega', 'height' => 600, 'width' => 50)
    );
}

On the controller:

<?php

class ExampleController extends AppController {
    public $components = array('Paginator');
    public $uses = array('Fake');

    public function justatest() {
        $this->Paginator->settings = array(
            'order' => array('id' => 'desc'),
            'limit' => 2
        );
        $records = $this->Paginator->paginate('Fake');
        $this->set(compact('records')); 
    }

Now, the data is retrieved correctly by the component. The "limit" condition works correctly. What doesn't work is the "order" condition: it doesn't work or the condition that I have indicated, or sort the data according to user input.

I cannot understand if I've done something wrong or if I cannot sort the data obtained with ArraySource.


EDIT The view:

<table>
    <tr>
        <th><?php echo $this->Paginator->sort('id'); ?></th>
        <th><?php echo $this->Paginator->sort('name'); ?></th>
        <th><?php echo $this->Paginator->sort('height'); ?></th>
        <th><?php echo $this->Paginator->sort('width'); ?></th>
    </tr>
    <?php foreach($records as $v): ?>
    <tr>
        <td><?php echo $v['Fake']['id']; ?></td>
        <td><?php echo $v['Fake']['name']; ?></td>
        <td><?php echo $v['Fake']['height']; ?></td>
        <td><?php echo $v['Fake']['width']; ?></td>
    </tr>
    <?php endforeach; ?>
</table>
1

1 Answers

0
votes

Right from the top of my head, try this :

$this->Paginator->settings = array(
        'order' => array('id DESC'),
        'limit' => 2
    );

Not sure at all