1

I have array that I have to pass into pagination. So how can i convert the array to Object ORM to set into pagination?

I can't $this->paginate = []; because I have to apply some foreach and conditions that's why i did first find all.

            $allProductData = $this->PurchaseRequisitionProducts->find('all',[
                'contain' => ['PurchaseRequisition','PurchaseOrderProducts'=>function($c){
                if($c==null){ return null; }
                return $c->where(['PurchaseOrderProducts.id IS NOT NULL']);
                },'PurchaseOrderProducts.PurchaseOrder'=>function($p){
                    return $p->where(['PurchaseOrder.id IS NOT NULL'])
                    ->where(['PurchaseOrder.is_approve'=>"Y"])
                    //->where(['PurchaseOrder.status'=>1])
                    ->where(['PurchaseOrder.po_type'=>1]);
                },'PurchaseOrderProducts.PurchaseOrder.CompanyMaster'] ,
                'conditions'=>[$condn,$conditions,'PurchaseRequisition.ready_for_inquiry'=>'Y','PurchaseRequisition.owner_company_id'=>$ownercomp],
                'order'=>["PurchaseRequisitionProducts.id"=>"desc"],
                ])->toArray();
                if($allProductData){
                    foreach($allProductData as $key1=>$prProd){                            
                        if(isset($prProd['purchase_order_products'])){
                            $supplier=[];
                            foreach($prProd['purchase_order_products'] as $key2=>$poProd){
                              $supplier[]=$poProd['purchase_order']['supplier_id'];
                                //debug($supplier);
                                $companies= $this->CompanyMaster->find('list', [
                                    'keyField' => 'id','valueField' => 'Company_name',
                                    'conditions'=>['id IN'=>$supplier],
                                ])->toArray();
                                $allProductData[$key1]['supplier']=$companies;
                            }

                        }
                    }
                }

   $pr_product = $this->paginate($allProductData)
Devendra
  • 219
  • 2
  • 22
  • Your `foreach` loop looks at first glance like it's basically adding more information into the results, which could perhaps be added in the original query via additional containment? If so, do that instead, and pass the result of the `find` (with no `toArray`) to the `paginate` call. If not, do the pagination before the `foreach`? – Greg Schmidt Oct 07 '18 at 15:44
  • @GregSchmidt i can do pagination before foreach but the pagination count is showing wrong because also i am also doing unset the array. So any ways to convert array to object to pass into pagination? – Devendra Oct 07 '18 at 16:18
  • Not clear on what you mean about unset; I don't see any of that in the code above. Are you just restating your [earlier question](https://stackoverflow.com/questions/52620028/convert-array-to-cake-orm-query-object-in-cakephp-3)? – Greg Schmidt Oct 07 '18 at 21:05
  • 1
    The short answer to both questions is "you can't". That's not how it works. Cake wants to be given a query which it can do a count on and apply limits to. This way, it doesn't have to read potentially thousands of records (as your `toArray` call will force) just to display 10. Build your query so that it inherently excludes the records you don't want and then let the pagination do its job. Given the right input, it will give you correct page counts and only have to read in precisely the records it's going to display. Think about how to change your question into one about how to fix your query. – Greg Schmidt Oct 07 '18 at 21:16

0 Answers0