Zend Select - 使用具有多种变体的相同选择

时间:2017-01-06 13:47:21

标签: php zend-framework zend-db

我在下面使用此代码。如您所见,对象$ oSelectE,$ oSelectNV和$ oSelectPV具有相同的基本选择查询,直到我添加where,group和order变体。

    $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');

所以,我尝试过,只使用一次选择,就像这样(在添加WHERE变体之前):

$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"); 

但是当我使用这个WHERE变体时发生了什么:

$oSelectNV->where("o.np = 'NV'")
        ->group('u.id')
        ->order('kz DESC');

条件也应用于对象$ oSelectNP(我可以看到在发送给DB的SQL中)。我不明白这个的逻辑。我想当启动$ oSelectE = $ oSelectNV = $ oSelectPV =时,只复制这些变量的值,所以当我使用WHERE函数时,它应该仅应用于$ oSelectNV而不应用于oSelectPV。这是一些ZEND错误,还是有逻辑解释?

1 个答案:

答案 0 :(得分:1)

可能只是引用克隆的对象。您可以尝试使用“clone”关键字来克隆整个对象;比如$ oSelectE = clone $ oSelectPV。