duanchun1909 2019-02-19 07:38
浏览 45

在zend框架中使用复杂的条件

I have a static function in which I am passing some variables from the controller, In the function, I am doing some DB table joins and some other queries, the values I am passing is getting from the filters, If I am not selecting any filters I will get null values.

public static function Myfunction($var1,$var2,$var3)
{
 ...some database queries..
->joinLeft(some conditions)  
->joinLeft(some conditions)
->where('table1.val1 = ?',$var1)
->where('table2.val2 = ?', $var2)
->where('table3.val3 =?' ,$var3);

 return $data
}

Here If the values I am passing is null then I don't need to check the first condition, similarly in the case of 2nd and 3rd if the value is null then I don't need to check any conditions and have to fetch the whole value from the DB as per the query, somebody, please help me how to do that in Zend?

  • 写回答

1条回答 默认 最新

  • dsfdsfdsfdsf1223 2019-06-05 15:40
    关注

    I use a php Closure for this case to encapsulate the condition-checking as follows:

    public static function Myfunction($var1,$var2,$var3)
    {
        return $this->tableGateway->select(function (Select $select) use ($var1, $var2, $var3) {
                ...some database queries..
                $select->joinLeft(some conditions)  
                $select->joinLeft(some conditions);
    
                if (!empty($var1)) {
                        $select->where('table1.val1 = ?',$var1);
                }
    
                if (!empty($var2)) {
                        $select->where('table2.val2 = ?', $var2);
                }
    
                if (!empty($var3)) {
                        $select->where('table3.val3 =?' ,$var3);
                }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度