2

I'm trying to paginate a union in cakephp 3. I have already found and read How do you modify a UNION query in CakePHP 3?, but am probably too new to cakephp to understand properly what is meant by "use a custom finder". I have read the relevant section in the cakephp book, but still fail to see how this helps to apply the epilog() to the query. Maybe someone could show a complete example? I have two models Customers and Suppliers. I am trying to builds a search function that returns matching entries from both models, including pagination and sorting.

        public function index($q=null) {
            $this->loadmodel('Suppliers');
            $this->loadmodel('Customers');
            $customers=$this->Customers->find('all')->select(['type'=>"'Customers'",'id'=>'id','name'=>'name','phone'=>'phone'])
                    ->where(['name like'=>'%'.$q.'%']);
            $suppliers=$this->Suppliers->find('all')->select(['type'=>"'Suppliers'",'id'=>'id','name'=>'name','phone'=>'phone'])
                    ->where(['name like'=>'%'.$q.'%']);
            $customers->union($suppliers);
            $results=$this->paginate($customers);
            $this->set(compact(['q','results']));
    }

As detailed in How do you modify a UNION query in CakePHP 3?, this results in the order,limit and offset only being applied to the first select. Removing the paginate() and applying order,limit and offset manually via epilog() works, but than I'd have to recreate the whole pagination mechanism in my code. While this is my current plan in case I can't find a better solution, I don't believe this would be the right way to do it.

Could someone (@PhantomWatson, @ndm) show an example, how the suggested custom finder can solve this?

Community
  • 1
  • 1
fpet
  • 73
  • 8
  • To my knowledge, the only way you are going to get this to work is to create your own paginating mechanism by using the link you already supplied or one of your own. – chrisShick Mar 28 '16 at 16:57
  • Uh, yes, but... As I said in the original question, I have no idea how to solve this by using a custom finder. Thus I was asking for an example. – fpet Mar 30 '16 at 11:15

0 Answers0