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)
foreachloop 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 thefind(with notoArray) to thepaginatecall. If not, do the pagination before theforeach?toArraycall 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.