3

I'm trying to fetch the data with HAVING clause.

Here is my code,

//Main Table
$select->from(array('cre' => 'credit'));
//company Table
$select->join(array('com' => 'company), 'cre.account_id = com.account_id');
$select->join(array('u' => 'user'), 'cre.approved_by = u.id', array('first_name', 'last_name'), 'left');
$select->join(array('acc' => 'account'), 'cre.account_id = acc.id', array('id'), 'left');

$startDate = \DateTime::createFromFormat('d-m-Y H:i:s', $startDate.' 00:00:00')->format('Y-m-d H:i:s');
$endDate = \DateTime::createFromFormat('d-m-Y H:i:s', $endDate.' 23:59:59')->format('Y-m-d H:i:s');
$between = new Zend\Db\Sql\Predicate\Between('cre.'.$filter, $startDate, $endDate);

$select->having
        ->nest
        ->equalTo('cre.paid', 0)
        ->and
        ->equalTo('cre.status', 'approved')
        ->and
        ->greaterThanOrEqualTo('cre.due_date', $currentDate)
    ->unnest
    ->or
    ->nest
        ->equalTo('cre.account_id', "acc.id")
        ->and
        ->equalTo('com.account_id', "acc.id")
        ->and
        ->greaterThanOrEqualTo('com.credit_max_amount', 'sum(cre.amount)')
        ->and
        ->equalTo('cre.paid', 0)
        ->and
        ->equalTo('cre.status', 'approved')
    ->unnest;

This is producing the right query for me. And after $select->getSqlString(); The produced query successfully runs in my phpmyadmin.

But when I actually run the code, Error shows,

Statement couldn't be produced with sql: SELECT COUNT(1) AS C FROM (SELECT com.name AS com_name, cre.amount AS cre_amount, cre.status AS cre_status, cre.paid AS cre_paid, cre.created_on AS cre_created_on, cre.approval_date AS cre_approval_date, u.first_name AS u_first_name, u.last_name AS u_last_name, CONCAT(u.first_name, " ", u.last_name) AS approved_by, cre.due_date AS cre_due_date, cre.id AS cre_id FROM credit AS cre INNER JOIN company AS com ON cre.account_id = com.account_id LEFT JOIN user AS u ON cre.approved_by = u.id LEFT JOIN account AS acc ON cre.account_id = acc.id WHERE cre.created_on BETWEEN ? AND ? HAVING (cre.paid = ? AND cre.status = ? AND cre.due_date >= ?) OR (cre.account_id = ? AND com.account_id = ? AND com.credit_max_amount >= ? AND cre.paid = ? AND cre.status = ?)) AS original_select

What is the problem? Where am I wrong?

Keyur
  • 1,113
  • 1
  • 23
  • 42

0 Answers0