Is it Possible to do a datatable search using the generated sumcount of the sum case in CAKEPHP

Viewed 18

I want to do a search in the serverside datatable with the number of carried and uncarried cases. how can I use the cases in the search condition. the cases is the result of a total count of carried and uncarried cases of each branch code. If i search 40 the branch code that has 40 entries shouldd appear. I dont know if this is possible in the serverside or nah.

$query = $this->find();
    $carriedCase = $query->newExpr()
        ->addCase(
            $query->newExpr()->add(['prodCarried.carried' => '1']),
            1,
            'integer'
        );
    $unCarriedCase = $query->newExpr()
        ->addCase(
            $query->newExpr()->add(['prodCarried.carried' => '0']),
            1,
            'integer'
        );
    //disctinct code
    $query ->select([
            'name',
            'code',
            'carried' => $query->func()->count($carriedCase),
            'unCarried' => $query->func()->count($unCarriedCase),
            'prodCarried.id',
            'prodCarried.validity_start',
            'prodCarried.validity_end',
    ]);
    $query ->distinct([
        'code'
    ]);
     $query->join([
      'prodCarried' => [
        'table' =>'products_carried_per_branches',
        'type' => 'inner',
        'conditions' => [
          'prodCarried.company_id = Branches.company_id',
          'prodCarried.branch_code = Branches.code',
        ]
      ]
    ]);

    $query->where(function (QueryExpression $exp, Query $q) use($option,$search){
        $exp->eq('Branches.company_id', $option['company_id']);
        $exp->eq('Branches.deleted', 0);
        $exp->eq('prodCarried.deleted', 0);
        if(!empty($search)){
            $orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
                $or->like('name', "%$search%");
                $or->like('code', "%$search%");

                return $or;
            });
            $exp->add($orConditions);
        }
    return $exp;
    });
0 Answers
Related