在cakephp 3中将数组转换为Cake \ ORM \ Query对象

时间:2018-10-03 05:07:51

标签: cakephp cakephp-3.0 cakephp-3.x

在这里,我正在将$allProductData转换为数组,因为必须在其中应用foreach循环和某些条件,然后才将$allProductData分配给分页,但是却出现错误“无法找到与分页兼容的对象。”那么如何将数组转换为Cake\ORM\Query对象以传递到分页中呢?

$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 个答案:

答案 0 :(得分:0)

与其在查找所有查询中添加数组,不如在分页结果中添加它,例如:

Seat.genSeat(x, row, col)

或者您可以尝试这样:

Seat.genSeat(x, 32, 8)

答案 1 :(得分:0)

您也可以这样操作,必须删除toArray()才能使分页工作。

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

还要定义此公共变量

public $paginate = [
    'limit' => 25,
    'order' => [
        'TableName.columnName' => 'asc' //not mandatory
    ]
];