Bhavesh Chauhan
Bhavesh Chauhan

Reputation: 1053

How to use joined model for sorting in cakephp 3

I am using pagination component for sorting in cakephp3

<?php echo $this->Paginator->sort('field');?>

In model i have joined multiple models

        $this->table('orders');
        $this->primaryKey('order_id');
        $this->hasMany('Customers');
        $this->belongsTo('Customers', [
               'foreignKey' => 'fk_customerid',
               'joinType' => 'LEFT',
               'dependent' => true,
           ]);
        $this->hasMany('Users');
        $this->belongsTo('Users', [
            'foreignKey' => 'fk_userid',
            'joinType' => 'LEFT',
            'dependent' => true,
        ]);

In Controller -> public function index has

$getOrders = $this->Orders->find('all')
                ->contain([
                    'Customers' => function ($q) {
                        return $q
                            ->select(['customer_name']);

                    },
                    'Users' => function ($q) {
                        return $q
                            ->select(['firstname','lastname']);
                    }
                ])
                ->limit($this->per_page);

Then how to set sorting for customer name using paginate component.thanks

Upvotes: 0

Views: 121

Answers (1)

Krunal Dave
Krunal Dave

Reputation: 272

In cakephp3 for associated models sorting use 'sortWhitelist' in controller:

$this->paginate = ['sortWhitelist' => [comma seprated names of your fiels],'limit' =>give pagelimit];

 $this->set('getorder', $this->paginate($getOrders));

Upvotes: 1

Related Questions