gpt4 book ai didi

php - Laravel5 Eloquent 查询什么都不返回,但原始查询却返回?

转载 作者:行者123 更新时间:2023-11-29 22:18:22 25 4
gpt4 key购买 nike

更新根据:

    $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 中回显):

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

问题在于 eloquent 返回零行。而在 Putty session 中运行时,查询本身会返回超过 50 个(?!)。我不知道为什么 eloquent 找不到这些行?我读过一些讨论,认为 whereRaw 有点碰运气,但找不到任何具体的证据来支持这一点。大家有什么想法吗?

最佳答案

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

// 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));
}

关于php - Laravel5 Eloquent 查询什么都不返回,但原始查询却返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com