You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony Doctrine报错:stdClass对象无法转换为字符串

Fix: Object of class stdClass could not be converted to string in QueryBuilder

What's Causing the Error?

The problem jumps right out in your getDateRange() method: you’re passing the entire $results array (a collection of stdClass objects from your API) directly to the :id parameter with setParameter('id', $results).

When Doctrine’s QueryBuilder tries to translate this into SQL, it can’t convert an array of objects into a string for the t.id >= :id condition—hence the error you’re seeing.

Two Possible Fixes (Based on Your Actual Goal)

Let’s break down the two most likely things you’re trying to do, and how to fix each:


Fix 1: Query the Database for Records Matching API IDs + 6-Month Date Range

If you want to pull database records that have IDs present in your API results AND were created in the last 6 months, you need to:

  1. Extract just the IDs from your API’s $results objects
  2. Use a SQL IN clause instead of >= to match multiple IDs

Here’s the updated service layer code:

public function getDateRange($results) {
    // Step 1: Extract all IDs from the API's stdClass objects
    $ids = array_column($results, 'id');
    
    // Guard against empty ID array to avoid invalid SQL
    if (empty($ids)) {
        return [];
    }

    return $this->getNamesRepository()
        ->createQueryBuilder('t')
        ->where('t.created >= :end')
        ->andWhere('t.id IN (:ids)') // Use IN for multiple IDs
        ->setParameter('end', new \DateTime('-6 month'))
        ->setParameter('ids', $ids) // Pass the array of IDs, not the full objects
        ->orderBy('t.id', 'desc')
        ->getQuery()
        ->getArrayResult();
}

And update your controller to use the returned database results instead of raw API data:

$getResults = $this->getAllNames()->getContent();
$pullResults = json_decode($getResults);
$apiResults = $pullResults->data->items;

// Get filtered database records
$filteredResults = $this->get('my.service')->getDateRange($apiResults);

$rows = [];
$rows[] = ["First Name", "Last Name"];
foreach ($filteredResults as $row) {
    $rows[] = [$row['firstName'], $row['lastName']]; // Note: getArrayResult() returns arrays, not objects
}

Fix 2: Filter API Results In-Memory (No Database Query Needed)

If you actually want to filter the existing API results to only include items created in the last 6 months (instead of hitting the database again), you don’t need QueryBuilder at all. Just use array_filter() to process the API data directly:

Update your service layer (or do this directly in the controller):

public function filterDateRange($results) {
    $sixMonthsAgo = new \DateTime('-6 month');
    
    // Filter the API results to keep only items created in the last 6 months
    return array_filter($results, function($item) use ($sixMonthsAgo) {
        // Convert the item's created date to a DateTime object (adjust format if needed)
        $itemCreated = new \DateTime($item->created);
        return $itemCreated >= $sixMonthsAgo;
    });
}

Then in your controller:

$getResults = $this->getAllNames()->getContent();
$pullResults = json_decode($getResults);
$results = $pullResults->data->items;

// Filter API results in memory
$filteredResults = $this->get('my.service')->filterDateRange($results);

$rows = [];
$rows[] = ["First Name", "Last Name"];
foreach ($filteredResults as $row) {
    $rows[] = [$row->firstName, $row->lastName]; // Still using stdClass objects
}

Key Takeaway

Always double-check what you’re passing to QueryBuilder parameters—if you’re working with collections, use IN clauses with ID arrays, not full objects. If you’re just filtering existing data, skip the database query entirely for better performance.

内容的提问来源于stack exchange,提问作者develops

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:25:52