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 (SELECTcom
.name
AScom_name
,cre
.amount
AScre_amount
,cre
.status
AScre_status
,cre
.paid
AScre_paid
,cre
.created_on
AScre_created_on
,cre
.approval_date
AScre_approval_date
,u
.first_name
ASu_first_name
,u
.last_name
ASu_last_name
, CONCAT(u.first_name, " ", u.last_name) ASapproved_by
,cre
.due_date
AScre_due_date
,cre
.id
AScre_id
FROMcredit
AScre
INNER JOINcompany
AScom
ONcre
.account_id
=com
.account_id
LEFT JOINuser
ASu
ONcre
.approved_by
=u
.id
LEFT JOINaccount
ASacc
ONcre
.account_id
=acc
.id
WHEREcre
.created_on
BETWEEN ? AND ? HAVING (cre
.paid
= ? ANDcre
.status
= ? ANDcre
.due_date
>= ?) OR (cre
.account_id
= ? ANDcom
.account_id
= ? ANDcom
.credit_max_amount
>= ? ANDcre
.paid
= ? ANDcre
.status
= ?)) ASoriginal_select
What is the problem? Where am I wrong?