簡體   English   中英

語法錯誤或訪問沖突:1064 | 使用 Doctrine (QueryBuilder) 發送數組

[英]Syntax error or access violation: 1064 | Sending array using Doctrine (QueryBuilder)

我來找你是因為我在 POST 請求中發送數組時遇到問題。 我在 Symfony 4.3.11 中創建了一個 RESTAPI。

如果我在每個數組中只發送 1 個字符串,則此代碼有效:

            $repository = $this->getDoctrine()->getRepository(Application::class);

            $query = $repository->createQueryBuilder('request');
            $query
                ->andWhere($query->expr()->Andx(
                    $query->expr()->eq('request.curr', ':curr'),
                    $query->expr()->eq('request.country', ':JSONcountry'),
                    $query->expr()->eq('request.type', ':JSONtype'),
                    $query->expr()->eq('request.job', ':JSONjob'),
                ))
                ->setParameter('curr', true)
                ->setParameter('JSONcountry', $app->getCountry())
                ->setParameter('JSONtype', $app->getType())
                ->setParameter('JSONjob', $app->getJob());

            echo $query;
            $query = $query->getQuery();
            $applications = $query->getResult();

            return $applications;

但是當我嘗試發送這種類型的 JSON 時:

{
    "caisse":["FRANCE", "ESPAGNE"],
    "type":["FR", "ES"],
    "job":["CHIEF", "TEST"]
}

它返回此錯誤:

    "code": 500,
    "message": "An exception occurred while executing 'SELECT ...' with params [1, \"FRANCE\", \"ESPAGNE\", \"FR\", \"ES\", \"CHEF\", \"TEST\"]:\n\nSQLSTATE[42000]: 
    Syntax error or access violation: 1064"

}

我在網上搜索了錯誤,發現這個錯誤經常在語法錯誤或缺少某些內容時顯示,但在我的情況下,代碼正在運行。

我還有第二個問題:就我而言,如何為數組中的每個字符串執行“LIKE”運算符? 我已經看到了一種方法,但僅適用於字符串: ->setParameter('JSONcountry', '%' . $app->getCountry() . '%')

如果您對我的代碼有任何問題或建議,請隨時提出。

謝謝。

如果你的參數是一個數組,你應該使用 $query->expr()->in(...) 函數而不是 equal 。 我假設您正在尋找與數組中的任何元素匹配的對象。

$repository = $this->getDoctrine()->getRepository(Application::class);

$query = $repository->createQueryBuilder('request');
$query
    ->andWhere($query->expr()->Andx(
        $query->expr()->eq('request.curr', ':curr'),
        $query->expr()->in('request.country', ':JSONcountry'),
        $query->expr()->in('request.type', ':JSONtype'),
        $query->expr()->in('request.job', ':JSONjob'),
   ))
   ->setParameter('curr', true)
   ->setParameter('JSONcountry', $app->getCountry())
   ->setParameter('JSONtype', $app->getType())
   ->setParameter('JSONjob', $app->getJob());

echo $query;
$query = $query->getQuery();
$applications = $query->getResult();

return $applications;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM