Remove Parent array if contain is empty in cakephp 3

208 Views Asked by At

I have to remove the parent array if the contain is not available or empty. In below code if supplierOffer is empty then I have to remove the SupplierInquiry array but here I am getting the SupplierInquiry array with empty supplierOffer. I cann't unset by using foreach loop because then the count will show wrong. Any solution for this issue?

    $this->paginate =   [
        'contain'=>['SupplierOffer'=>function($q){
            return $q->where(['SupplierOffer.status IS NULL']);
        },'CompanyMaster','SupplierOffer.PurchaseOrder','SupplierOffer.SupplierOfferProducts','SupplierOffer.SupplierOfferProducts.ProductsMaster','SupplierOffer.SupplierOfferProducts.Uom','SupplierOffer.SupplierOfferProducts.Currency', 'OwnerCompanies','SupplierOffer.CompanyMaster','SupplierOffer.SupplierOfferProducts.PrSuppliers'],
            'order'=>['SupplierInquiry.id' => 'DESC'],
            'conditions'=>[$condn,$conditions,'SupplierOffer IS NOT NULL'],
    ];
    $supplierOffer = $this->paginate($this->SupplierInquiry);
1

There are 1 best solutions below

0
Rakesh On

You need to use Inner join and pu the condition on child table. And also you need to specify fields you want to fetch.

$query = $this->SupplierInquiry->find()->where(['SupplierOffer.status IS NOT' => NULL])->hydrate(false)->join([
    'table' => 'supplier_offer',
    'alias' => 'SupplierOffer',
    'type' => 'INNER',
    'conditions' => 'SupplierOffer.supplier_inquiry_id = SupplierInquiry.id'
])->select(' All fields you required ');