2017-09-06 4 views
1

Je travaille sur un projet qui utilise la doctrine et Symfony. Actuellement, j'ai un problème lorsque j'essaie d'utiliser l'annotation en cascade entre deux entités avec une relation bidirectionnelle.Erreur dans la relation bidirectionnelle

J'ai deux entités.

MediaIndexer:

class MediaIndexer { 
    /** 
    * @var int 
    * @ORM\Column(type="guid") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    */ 
    private $id; 

    /** 
    * @var ArrayCollection $identifiers 
    * @ORM\OneToMany(targetEntity="CelebrityBundle\Entity\MediaIdentifier", 
    *  mappedBy="indexer", 
    *  cascade={"all"}, 
    *  orphanRemoval=true) 
    */ 
    private $identifiers; 

    // ... 

    public function setValue($key, $value) { 
     foreach ($this->getIdentifiers() as $identifier) { 
      if ($identifier->getKey() == $key) { 
       $identifier->setValue($value); 

       return $this; 
      } 
     } 

     $identifier = new MediaIdentifier(); 
     $identifier->setKey($key) 
        ->setValue($value) 
        ->setIndexer($this); 

     $this->addIdentifier($identifier); 

     return $this; 
    } 
} 

MediaIdentifier:

class MediaIdentifier { 
    /** 
    * @var int 
    * @ORM\Column(type="guid") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    */ 
    private $id; 

    /** 
    * @var MediaIndexer $indexer 
    * @ORM\ManyToOne(targetEntity="CelebrityBundle\Entity\MediaIndexer", inversedBy="identifiers") 
    * @ORM\JoinColumn(onDelete="CASCADE", referencedColumnName="id") 
    */ 
    private $indexer; 

    /** 
    * @var string $content 
    * @ORM\Column(name="key", type="string", length=255, nullable=false) 
    */ 
    private $key; 

    /** 
    * @var int $type 
    * @ORM\Column(name="value", type="text", nullable=false) 
    */ 
    private $value; 
    // ... 
} 

(En fait, ces classes sont utilisées pour construire un "tableau associatif" dans DB).

J'ai essayé cette relation avec un peu de code:

public function testAction() 
{ 
    $indexer = new MediaIndexer(); 

    $indexer->setValue("screenName", "katyperry"); 
    $indexer->setValue("pageId", "00000"); 

    $this->getDoctrine()->getManager()->persist($indexer); 
    $this->getDoctrine()->getManager()->flush(); 
} 

Et, quand je lance ce code, j'ai l'erreur:

An exception occurred while executing 'INSERT INTO medias_identifiers (id, key, value, indexer_id) VALUES (?, ?, ?, ?)' with params ["b8a4c4e3-9312-11e7-93c0-e55e28abe26b", "screenName", "katyperry", "b8a47f19-9312-11e7-93c0-e55e28abe26b"]: 

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 'key, value, indexer_id) VALUES ('b8a4c4e3-9312-11e7-93c0-e55e28abe26b', 'screenN' at line 1 

Avez-vous une idée sur cette question ?

Merci

Répondre

3

KEY est un reserved word in MariaDB. Essayez d'entourer la définition de clé avec backticks:

/** 
* @var string $content 
* @ORM\Column(name="`key`", type="string", length=255, nullable=false) 
*/ 
private $key; 
+0

OMG. Je suis dessus lundi! Merci beaucoup: p! – graille

+0

Pas de problème! Ça nous arrive à tous –