繁体   English   中英

如何检查表是否已经在 Laravel 查询生成器中有一个 where 子句?

[英]How to check if table is already has a where clause in Laravel Query Builder?

是一个示例,说明我们如何检测表是否已加入 Laravel 中的查询。

public static function isJoined($query, $table)
    {
        $joins = $query->getQuery()->joins;
        if($joins == null) {
            return false;
        }

        foreach ($joins as $join) {
            if ($join->table == $table) {
                return true;
            }
        }

        return false;
    }

我需要的是提取查询中的where子句。

我有一个包含一些join和一些where条件的查询,我需要在嵌套的 select 中使用其中一个where条件。

现在我需要的是从查询中提取 where 子句并再次使用它。 问题是我如何确定我的查询是否包含我的 Eloquent 查询中表的特定列(例如org.id )的任何where条件?

我尝试按如下方式从查询中提取wheres但不像我们的连接

$wheres = $query->getQuery()->wheres;
  foreach ($wheres as $where) {
    dd(array_keys($where));
  }

我收到的是:

array:3 [
  0 => "type"
  1 => "query"
  2 => "boolean"
]

type的值是nested的,如果我尝试以下代码:

$wheres = $query->getQuery()->wheres;
  foreach ($wheres as $where) {
     dd($where['query']->wheres);
  }

然后我有:

array:1 [
  0 => array:5 [
    "type" => "Basic"
    "column" => "org.path"
    "operator" => "LIKE"
    "value" => "/202001/10000000/12400000%"
    "boolean" => "and"
  ]
]

现在为什么第一个 wheres 返回不同的wheres 我希望在第一个$where中得到这个结果!

您可以使用查询生成器的->wheres属性执行几乎相同的操作。 它将包含以下格式的 where 子句数组:

    ["type"] => string(5) "Basic"
    ["column"] =>string(1) "q"
    ["operator"] => string(1) "="
    ["value"] => int(2)
    ["boolean"] => string(3) "and"

由此看来,您可以使用column属性。

UPD:基本上你可以创建一个递归的 function 来为你检查它:

  ...
  $wheres = $query->getQuery()->wheres;
  foreach ($wheres as $where) {
     if( $checkColumn($where, 'myColumn) ) { 
        // found it
        break;
     }
  }

  function checkColumn($where, $column) {
     if( $where['type'] == 'nested' ) {
         return checkColumn($where['query'], $column);
     }
     else {
        return $where['column'] == $column;
    }
  }

这是我的递归 function 可以放在 Model class 中:

    /**
     * Returns the where conditions on a specific joined table?
     * @return array of wheres 
     */
    protected function wheres($query, $table){ 
        $conditions = [];
        if($query instanceof \Illuminate\Database\Eloquent\Builder){
            $wheres = $query->getQuery()->wheres;
        } elseif($query instanceof \Illuminate\Database\Query\Builder) {
            $wheres = $query->wheres;
        }

        if($wheres == null) {
            return [];
        }

        foreach ($wheres as $where) {
            if (isset($where['column']) && substr_compare($where['column'], $table, 0, strlen($table)) === 0){
                array_push($conditions, $where);
            } else if (isset($where['query'])){
                $conditions = array_merge($conditions, $this->wheres($where['query'], $table));
            }
        }
        return $conditions;
    }

它可以在控制器中调用,如下所示:

$wheres = $this->wheres($query, 'org');
foreach($wheres as $where){
   ....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM