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 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码