0

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

2 Answers 2

0

You can do it this way also, you have to remove toArray() in order to make pagination work.

$allProductData = $this->PurchaseRequisitionProducts->find('all')->contain(['PurchaseOrderProducts']);
$pr_product = $this->paginate($allProductData);

Also, define this public variable

public $paginate = [
    'limit' => 25,
    'order' => [
        'TableName.columnName' => 'asc' //not mandatory
    ]
];
Sign up to request clarification or add additional context in comments.

5 Comments

when I am doing find all after that I have apply some unset conditions in the find all then I have to assign the variable for paginate
unset conditions like? What do you mean by that?
when I am getting all data then I have to apply foreach loop and remove some of the array
you don't need to do that, define that variable in the class of your controller it will take care of it automatically. define limit according to the data you need.
I have add my complete code where I am doing too many unset under foreach loop can you please check.
0

Instead of adding to array in find all query you should add it on the result of pagination like:

   $allProductData = $this->PurchaseRequisitionProducts->find('all',[
        'contain' => ['PurchaseOrderProducts']
    ]);// remove toArray() from here
    $pr_product = $this->paginate($allProductData)->toArray(); // AddtoArray() here

OR you can try like this:

 $paginate = [
                    'contain' => ['PurchaseOrderProducts'],
                    'limit' => 10
                ];
    $this->set('data', $this->Paginator->paginate($this->PurchaseRequisitionProducts->find('all'), $paginate)->toArray());

7 Comments

But before to add on pagination I have do some unset conditions in the find all.
What do you want to unset?
I have add my complete code where I am doing too many unset under foreach loop can you please check.
You can pass these conditions in query. Anyway just remove toArray() from the $allProductData and set pagination data like $this->set('paginationData', $pr_product); after unsetting variables.
ok sure, and you dont need to unset the data, you can use the column name in condition directly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.