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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化