PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] How to use joined model for sorting in cakephp 3

 January 17, 2022     cakephp-3.0     No comments   

Issue

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


Solution

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


Answered By - Krunal Dave
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing