Laravel5 eloquent查询什么都不返回,但原始查询呢?

时间:2015-06-23 07:24:27

标签: php mysql eloquent laravel-5

更新根据:

    $queries = DB::getQueryLog();
    $last_query = end($queries);

    var_dump($queries);

Laravel未运行任何疑问:array(0) {}

原帖

我有这个方法(请忽略所有评论 - 我正在调试):

public function runForResultSet(){
    $params = [];

    $query = Recipient::with('recipientTags', 'contactDetails', 'recipientTags.tagGroups');
    foreach($this->segmentCriteria as $criteria){
        $parts = explode('.', $criteria['field']);
        $fieldObject = SegmentTableWhiteListFields::where('field', '=', $parts[1])->get();
        foreach($fieldObject as $whiteList){
            $params[0] = [$criteria->value];
            $query->whereRaw(\DatabaseHelper::generateOperationAsString($whiteList, $parts, $criteria), $params[0]);
        }
    }

    $collection = $query->get();
    // $collection = $collection->filter(
    //  function($response) {
    //      $returnValues = [];
    //      foreach($response->getRelations()['recipientTags'] as $recipientTags){
    //          foreach($this->segmentRecipientTagGroupCriteria as $criteria){
    //              // return only results that match our segment criterias - both tag group name
    //              // and tag group ids
    //              $returnValues[] = ((DatabaseHelper::determineAndRunOperation($recipientTags->name, $criteria->operator, $criteria->value))
    //                  && $recipientTags->recipient_tag_group_id == $criteria->recipient_tag_group_id);
    //
    //          }
    //      }
    //      return count(array_keys($returnValues, true)) == count($returnValues);
    //  }
    // );

    // return the response
    return response()->json($collection, \ApiResponse::$STATUS_OK);
}

generateOperationAsString方法:

public static function generateOperationAsString($whiteList, $parts, $criteria){
    switch($whiteList->datatype){
      case "VARCHAR":
      case "TEXT":
        switch(strtoupper($criteria->operator)){
          case "IS NULL":
          case "IS NOT NULL":
            return "`".$parts[1] . "` " . $criteria->operator;
          break;
          default:
            return "`".$parts[1] . "` " . $criteria->operator . " ?";
        }
        break;

      case "INT":
      case "DOUBLE":
      case "FLOAT":
      case "REAL":
        switch(strtoupper($criteria->operator)){
          case "IS NULL":
          case "IS NOT NULL":
            return "`".$parts[1] . "` " . $criteria->operator;
          break;
          default:
            return "`".$parts[1] . "` " . $criteria->operator . " ?";
        }
        break;

      // Possibly don't need to separate these, but...
      case "TIMESTAMP":
      case "DATETIME":
        switch(strtoupper($criteria->operator)){
          case "IS NULL":
          case "IS NOT NULL":
            return "`".$parts[1] . "` " . $criteria->operator;
          break;
          default:
            return "`".$parts[1] . "` " . $criteria->operator . " ?";
        }
        break;

      case "DATE":
        switch(strtoupper($criteria->operator)){
          case "IS NULL":
          case "IS NOT NULL":
            return "`".$parts[1] . "` " . $criteria->operator;
          break;
          default:
            return "`".$parts[1] . "` " . $criteria->operator . " ?";
        }
        break;
    }
  }

它生成的整个sql是(来自$ query的echo):

select * from `recipients` where `last_visit` is not null

麻烦的是,雄辩会返回零行。而在putty会话中运行时,查询本身返回超过50(?!)。我不知道为什么雄辩没有找到这些行?我已经读过一些喋喋不休的问题,whereRaw有点受伤,但找不到任何支持这一点的具体证据。有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

修正了它。在特定情况下,我的$criteria->value为空,特别是在构建动态IS NULL查询时,其值不适用。在这种情况下,whereRaw没有运行,因为参数传递为null / empty(错误消息会有用!)。我将上面的代码更改为:

// Need to check for empty value as laravel's whereRaw will not run if the provided
// params are null/empty - In which case we need to use whereRaw without params.
if(!empty($criteria->value)){
    $query->whereRaw(\DatabaseHelper::generateOperationAsString($whiteList, $parts, $criteria), $params[0]);
}else{
    $query->whereRaw(\DatabaseHelper::generateOperationAsString($whiteList, $parts, $criteria));
}