duandai2178 2017-01-06 13:47
浏览 47
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongsan6889 2017-01-11 13:01
    关注

    may be only reference to object is cloned. You can try to use "clone" keyword to clone whole object; Like $oSelectE = clone $oSelectPV .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码