I am using this code below. As you can see, objects $oSelectE, $oSelectNV and $oSelectPV have the same basic select query, until I add the where, group and order variations.
$oSelectE = $this->getAdapter()->select()->distinct()
->from(array('o' => 'odpocty'), array('u.id', 'kz' => 'SUM(o.kz)', 'obrat' => 'SUM(o.vyplata)'))
->join(array('u' => 'uzivatele'), 'u.id = o.zadalid', array('u.jmeno', 'u.prijmeni'))
->where("o.orgid=" . intval($orgid))->where("o.datum>='" . $od . "'")->where("o.datum<='" . $do . "'")->where("o.kz<>0");
$oSelectNV = $this->getAdapter()->select()->distinct()
->from(array('o' => 'odpocty'), array('u.id', 'kz' => 'SUM(o.kz)', 'obrat' => 'SUM(o.vyplata)'))
->join(array('u' => 'uzivatele'), 'u.id = o.zadalid', array('u.jmeno', 'u.prijmeni'))
->where("o.orgid=" . intval($orgid))->where("o.datum>='" . $od . "'")->where("o.datum<='" . $do . "'")->where("o.kz<>0");
$oSelectPV = $this->getAdapter()->select()->distinct()
->from(array('o' => 'odpocty'), array('u.id', 'kz' => 'SUM(o.kz)', 'obrat' => 'SUM(o.vyplata)'))
->join(array('u' => 'uzivatele'), 'u.id = o.zadalid', array('u.jmeno', 'u.prijmeni'))
->where("o.orgid=" . intval($orgid))->where("o.datum>='" . $od . "'")->where("o.datum<='" . $do . "'")->where("o.kz<>0");
$oSelectE->group('u.id')
->order('kz DESC');
$oSelectNV->where("o.np = 'NV'")
->group('u.id')
->order('kz DESC');
$oSelectPV->where("o.np = 'PV'")
->group('u.id')
->order('kz DESC');
So, what I tried, was use the select only once, like this (before adding the WHERE variations):
$oSelectE = $oSelectNV = $oSelectPV = $this->getAdapter()->select()->distinct()
->from(array('o' => 'odpocty'), array('u.id', 'kz' => 'SUM(o.kz)', 'obrat' => 'SUM(o.vyplata)'))
->join(array('u' => 'uzivatele'), 'u.id = o.zadalid', array('u.jmeno', 'u.prijmeni'))
->where("o.orgid=" . intval($orgid))->where("o.datum>='" . $od . "'")->where("o.datum<='" . $do . "'")->where("o.kz<>0");
But what happened was, when I used this WHERE variation:
$oSelectNV->where("o.np = 'NV'")
->group('u.id')
->order('kz DESC');
The condition was also applied to the object $oSelectNP (I can see that in the SQL sent to the DB). I do not understand the logic of that. I suppose when initiating $oSelectE = $oSelectNV = $oSelectPV =, only the value of these variables is copied, so when I use the WHERE function, it should be applied only to $oSelectNV and NOT to oSelectPV. Is that some ZEND bug, or does this have a logical explanation?