簡體   English   中英

在原則中插入json不起作用

[英]Inserting a json in doctrine doesn't work

我調用一個JSON API,該API向我發送回數據,並且我想使用Symfony中的Doctrine將此JSON插入我的MariaDB數據庫中。

我檢索到的JSON是一個對象數組,我在互聯網上關注了幾個示例(示例: Doctrine array vs simple_array vs json_array ),但沒有任何效果,我不知道我的問題是什么。

這是我的代碼:

    $client = new Client();
    $request = $client->request('GET', 'mylink.com');
    $response = $request->getBody();
    $livescore = json_decode($response, true);

    $array = [];
    foreach($livescore as $value) {
        if($value['match_hometeam_name'] === 'Lyon' || $value['match_awayteam_name'] === 'Lyon') {
            $array = $value;
            break;
        }
    }

    $livescoreObj = new Livescore();
    $livescoreObj->setDateRafraichissement(new \DateTime());
    $livescoreObj->setMatch($array);

    $this->entityManager->persist($livescoreObj);
    $this->entityManager->flush($livescoreObj);

    return new JsonResponse($array);

我的實體:

    <?php

namespace SGBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Livescore
 *
 * @ORM\Entity()
 */
class Livescore
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", options={"unsigned":true}, nullable=false)
     *
     * @var int
     */
    private $id;

    /**
     * @ORM\Column(type="json_array", nullable=true)
     *
     * @var string
     */
    private $match;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime")
     */
    private $dateRafraichissement;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getMatch()
    {
        return $this->match;
    }

    /**
     * @param mixed $match
     */
    public function setMatch($match)
    {
        $this->match = $match;
    }

    /**
     * @return \DateTime
     */
    public function getDateRafraichissement()
    {
        return $this->dateRafraichissement;
    }

    /**
     * @param \DateTime $dateRafraichissement
     */
    public function setDateRafraichissement($dateRafraichissement)
    {
        $this->dateRafraichissement = $dateRafraichissement;
    }
}

我的錯誤:

SQLSTATE [42000]:語法錯誤或訪問沖突:1064'match,date_rafraichissement)附近的語法錯誤VALUES('{\\“ match_id \\”:\\“ 257194 \\”,\\“ country_id \\”:\\'在第1行

預先感謝您的幫助

您的問題是$match屬性: MATCH是MySQL中的保留字 ,在查詢中使用時必須加引號。

出於某些原因,Doctrine不會自動引用字段。 但是,您可以在構建查詢時告訴它使用字段名引用 請嘗試以下操作:

/**
 * @ORM\Column(name="`match`", type="json_array", nullable=true)
 *
 * @var string
 */
private $match;

暫無
暫無

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

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