Création de l'entité User et mise à jour de la base !

90608
,

Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

Créer une entité User.php

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $password;

    /**
 * @ORM\Column(type="json")
 */
 private $roles = [];

    /**
     * @Assert\NotBlank()
     * @Assert\Length(max=4096)
     */
    private $plainPassword;

    /**
     * @ORM\OneToMany(targetEntity=Facture::class, cascade={"persist", "remove"}, mappedBy="user")
     */
    private $facture;

    public function __construct()
    {
        $this->factures = new ArrayCollection();
    }


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }

    public function getUsername()
    {
        return $this->email;
    }

    public function getSalt()
    {
        return null;
    }

    // La méthode getPassword() est déjà implémentée
    //public function getPassword()
    //{
    //    return $this->password;
    //}

    public function getRoles()
    {
      $roles = $this->roles;
      // guarantee every user at least has ROLE_USER
      $roles[] = 'ROLE_USER';

      return array_unique($roles);
    }

    public function eraseCredentials()
    {
    }

    public function getFacture(): ?string
    {
        return $this->facture;
    }

    public function setFacture(string $facture): self
    {
        $this->facture = $facture;

        return $this;
    }


    public function addFacture(Facture $facture)
    {
        $this->factures->add($facture);
        $facture->setUser($this);
    }

}

Ce que je veux

J'aimerais pouvoir mettre à jour ma base de données avec ces 2 commandes
php bin/console doctrine:migration:diff
php bin/console doctrine:migration:migrate

Ce que j'obtiens cette erreur dans la console

Anouchka@Anouchka MINGW64 /c/xampp/htdocs/devis_facture (master)
$ php bin/console doctrine:migration:migrate

                Application Migrations

WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating up to 20190425202523 from 0

++ migrating 20190422172802

 -> CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, roles JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicod

e_ci ENGINE = InnoDB
Migration 20190422172802 failed during Execution. Error An exception occurred while executing 'CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, rol
es JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NULL, PR
IMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_un' at line 1

In AbstractMySQLDriver.php line 79:

An exception occurred while executing 'CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, roles JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTE
R SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NUL
L, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_un' at line 1

In PDOConnection.php line 90:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NUL
L, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_un' at line 1

In PDOConnection.php line 88:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NUL
L, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_un' at line 1

7 Réponse

169209
,

tu as quel version de MariaDB? c'est ta version de MariaDB qui fait en sorte que elle ne supporte pas le type JSON

90608
,

Je suis sur MySQL avec Xampp V3.2.2. Je ne sais pas pourquoi il parle de MariaDB !

5467
,

MariaDB est un fork de Mysql, les anciennes versions ne supportent pas le type json.
Tu peux remplacer l'annotation dans ton code :

@ORM\Column(type="json")
par
@ORM\Column(type="array")
169209
,

il te suffit de faire ce que olive140 dit ou bien mettre a jour la version de ta base de donnée et ça ira, tu es sur quel system ? windows? linux ou mac

17162
,

Bonjour.

Je suis sur MySQL avec Xampp V3.2.2. Je ne sais pas pourquoi il parle de MariaDB !

Tu devrais regarder ce qui inclues le téléchargement d'un logiciel avant d'éxécuter son installeur :

XAMPP Apache + MariaDB + PHP + Perl

Source : XAMPP Installers and Downloads for Apache Friends
Sinon pour ce qui concerne la version de MariaDB, ce serait là 10.1.38 et il est dit :

MariaDB 10.1 and above does not support MySQL 5.7's packed JSON objects. MariaDB follows the SQL standard and stores the JSON as a normal TEXT/BLOB. If you want to replicate JSON columns from MySQL to MariaDB, you should store JSON objects in MySQL in a TEXT column or use statement based replication. If you are using JSON columns and want to upgrade to MariaDB, you can either convert the JSON columns to TEXT or use mysqldump to copy these tables to MariaDB. In MySQL, JSON is compared according to json values. In MariaDB JSON strings are normal strings and compared as strings.

Source : MariaDB versus MySQL - Compatibility - MariaDB Knowledge Base » Incompatibilities between MariaDB 10.1 and MySQL 5.7

87112
,

Hello,
Effectivement, c'est un problème de version. Le type Json n'est pas disponible sur toutes les versions (ex: MySql 5.6 ne le supporte pas)
Ca peut se régler en configuration, c'est prévu dans Doctrine...
Dans config/packages/doctrine.yaml (Symfony 4):

doctrine:
    dbal:
        # Par exemple :
        server_version: mariadb-10.2.12

Plus d'infos sur la doc officielle : https://symfony.com/doc/current/reference/configuration/doctrine.html

90608
,

J'ai même supprimé l'attribut roles de type json et j'ai recrée un attribut roles de type array mais la migration bloque sur le fait que roles est de type json