Here I am converting $allProductData to array because there I have to apply foreach loop and some conditions after that I am assigning $allProductData to pagination but I am getting error "Unable to locate an object compatible with paginate." so how can I convert array to Cake\ORM\Query object to pass into pagination?
$this->PurchaseRequisitionProducts->hasMany('PurchaseOrderProducts', [
'bindingKey'=>'product_id',
"foreignKey"=>'product_id'
]);
$allProductData = $this->PurchaseRequisitionProducts->find('all',[
'contain' => ['PurchaseOrderProducts'=>function($c){
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.po_type'=>1])
->where(['PurchaseOrder.status'=>1]);
},'PurchaseOrderProducts.PurchaseOrder.CompanyMaster','PurchaseRequisition.Users.EmployeeDetails.Departments','ProductCategory','ProductSubcategory','Currency','PurchaseRequisition','ProductsMaster','Uom'] ,
'conditions'=>[$condn,$conditions],
//"sortWhitelist"=>["id",'EmployeeDetails.first_name','Departments.department',"pr_reference_no","pr_date",'purchase_requisition_id', "ProductsMaster.product_name","ref_price","qty","approved_qty"],
'order'=>["PurchaseRequisitionProducts.id"=>"desc"],
])->toArray();
$pr_product = $this->paginate($allData)->toArray();
if($pr_product){
foreach($pr_product as $key1=>$prProd){
if(empty($prProd['purchase_order_products']) || !isset($prProd['purchase_order_products']) || $prProd['purchase_order_products']==null || $prProd['purchase_order_products']=='' || empty($prProd['purchase_order_products'])){
unset($pr_product[$key1]);
}
if(isset($prProd['purchase_order_products'])){
$supplier=[];
$poarray=[];
foreach($prProd['purchase_order_products'] as $key2=>$poProd){
if($poProd['purchase_order']==null || $poProd['purchase_order']=='' || empty($poProd['purchase_order'])){
unset($prProd['purchase_order_products'][$key2]);
}
$supplier[]=$poProd['purchase_order']['supplier_id'];
//debug($supplier);
$companies= $this->CompanyMaster->find('list', [
'keyField' => 'id','valueField' => 'Company_name',
'conditions'=>['id IN'=>$supplier],
])->toArray();
$pr_product[$key1]['supplier']=$companies;
}
if(empty($prProd['supplier'])){
unset($pr_product[$key1]);
}
}
}
}