Valentin Harrang
Valentin Harrang

Reputation: 1191

Inserting a json in doctrine doesn't work

I call a JSON API that sends me data back and I want to insert this JSON into my MariaDB database using Doctrine in Symfony.

The JSON I retrieve is an array of objects, I followed several examples on the internet (Example: Doctrine array vs simple_array vs json_array ) but none works, I don't know what is my problem.

This is my code :

    $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);

My entity :

    <?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;
    }
}

My error :

SQLSTATE[42000]: Syntax error or access violation: 1064 Error syntax near 'match, date_rafraichissement) VALUES ('{\"match_id\":\"257194\",\"country_id\":\' at line 1

Thanks you in advance for your help

Upvotes: 3

Views: 7257

Answers (1)

lxg
lxg

Reputation: 13127

Your problem is the $match property: MATCH is a reserved word in MySQL and needs to be quoted when used in a query.

Doctrine does not autoquote fields, for reasons. But you can tell it to quote the field name when building a query. Try the following:

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

Upvotes: 7

Related Questions