2016-04-17 2 views
0
Doctrine2 symfonyでDBに

を要素を入れながら、:エラー私は質問タグ要素と呼ばれるテーブルに入れしようとしている

//the question with Id 1 exist already 
$question = new Question(); 
$question->setId(1); 

//the tag with name PYTHON exist already 
$tag = new Tag(); 
$tag->setName("PYTHON"); 

$questionTag = new QuestionTag(); 
$questionTag->setQuestion($question); 
$questionTag->setTag($tag); 

//now I call a service to put the item into DB 
$questionTagPF = $this->get('facade.QuestionTagFacade'); 
$res = $questionTagPF->create($questionTag); 

これは、エンティティを保存する方法であって、

public function create($entity) { 
    $this->entityManager->getConnection()->beginTransaction(); 
    try { 
     $this->entityManager->persist($entity); 
     $this->entityManager->flush(); 
     $this->entityManager->getConnection()->commit(); 
     return true; 
    } catch (Exception $e) { 
     $this->entityManager->getConnection()->rollBack(); 
     return false; 
    } 
} 

そして、これらは、エンティティクラスのとおりです。(質問とタグの間) 関係:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* QuestionTag 
* 
* @ORM\Table(name="question_tag", indexes={@ORM\Index(name="question", columns={"question"}), @ORM\Index(name="index_question_tag", columns={"tag"})}) 
* @ORM\Entity 
*/ 
class QuestionTag 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var \AppBundle\Entity\Question 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Question", cascade={ "persist" }) 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="question", referencedColumnName="id") 
    * }) 
    */ 
    private $question; 

    /** 
    * @var \AppBundle\Entity\Tag 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Tag", cascade={ "persist" }) 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="tag", referencedColumnName="name") 
    * }) 
    */ 
    private $tag; 



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

    /** 
    * Set question 
    * 
    * @param \AppBundle\Entity\Question $question 
    * 
    * @return QuestionTag 
    */ 
    public function setQuestion(\AppBundle\Entity\Question $question = null) 
    { 
     $this->question = $question; 

     return $this; 
    } 

    /** 
    * Get question 
    * 
    * @return \AppBundle\Entity\Question 
    */ 
    public function getQuestion() 
    { 
     return $this->question; 
    } 

    /** 
    * Set tag 
    * 
    * @param \AppBundle\Entity\Tag $tag 
    * 
    * @return QuestionTag 
    */ 
    public function setTag(\AppBundle\Entity\Tag $tag = null) 
    { 
     $this->tag = $tag; 

     return $this; 
    } 

    /** 
    * Get tag 
    * 
    * @return \AppBundle\Entity\Tag 
    */ 
    public function getTag() 
    { 
     return $this->tag; 
    } 
} 

質問エンティティ:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Question 
* 
* @ORM\Table(name="question", indexes={@ORM\Index(name="index_question_title", columns={"title"}), @ORM\Index(name="index_question_creation", columns={"creation_date"}), @ORM\Index(name="index_question_completed", columns={"completed"}), @ORM\Index(name="index_question_subject", columns={"subject"}), @ORM\Index(name="index_question_user", columns={"user_platform"})}) 
* @ORM\Entity 
*/ 
class Question 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="text", type="string", length=1000, nullable=true) 
    */ 
    private $text; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="creation_date", type="date", nullable=false) 
    */ 
    private $creationDate; 

    /** 
    * @var boolean 
    * 
    * @ORM\Column(name="completed", type="boolean", nullable=true) 
    */ 
    private $completed = '0'; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="level", type="integer", nullable=true) 
    */ 
    private $level = '1'; 

    /** 
    * @var \AppBundle\Entity\Subject 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Subject", cascade={ "persist" }) 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="subject", referencedColumnName="name") 
    * }) 
    */ 
    private $subject; 

    /** 
    * @var \AppBundle\Entity\UserPlatform 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\UserPlatform", cascade={ "persist" }) 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="user_platform", referencedColumnName="username") 
    * }) 
    */ 
    private $userPlatform; 


    /** 
    * Set id 
    * 
    * @param string $id 
    * 
    * @return Question 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

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

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return Question 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set text 
    * 
    * @param string $text 
    * 
    * @return Question 
    */ 
    public function setText($text) 
    { 
     $this->text = $text; 

     return $this; 
    } 

    /** 
    * Get text 
    * 
    * @return string 
    */ 
    public function getText() 
    { 
     return $this->text; 
    } 

    /** 
    * Set creationDate 
    * 
    * @param \DateTime $creationDate 
    * 
    * @return Question 
    */ 
    public function setCreationDate($creationDate) 
    { 
     $this->creationDate = $creationDate; 

     return $this; 
    } 

    /** 
    * Get creationDate 
    * 
    * @return \DateTime 
    */ 
    public function getCreationDate() 
    { 
     return $this->creationDate; 
    } 

    /** 
    * Set completed 
    * 
    * @param boolean $completed 
    * 
    * @return Question 
    */ 
    public function setCompleted($completed) 
    { 
     $this->completed = $completed; 

     return $this; 
    } 

    /** 
    * Get completed 
    * 
    * @return boolean 
    */ 
    public function getCompleted() 
    { 
     return $this->completed; 
    } 

    /** 
    * Set level 
    * 
    * @param integer $level 
    * 
    * @return Question 
    */ 
    public function setLevel($level) 
    { 
     $this->level = $level; 

     return $this; 
    } 

    /** 
    * Get level 
    * 
    * @return integer 
    */ 
    public function getLevel() 
    { 
     return $this->level; 
    } 

    /** 
    * Set subject 
    * 
    * @param \AppBundle\Entity\Subject $subject 
    * 
    * @return Question 
    */ 
    public function setSubject(\AppBundle\Entity\Subject $subject = null) 
    { 
     $this->subject = $subject; 

     return $this; 
    } 

    /** 
    * Get subject 
    * 
    * @return \AppBundle\Entity\Subject 
    */ 
    public function getSubject() 
    { 
     return $this->subject; 
    } 

    /** 
    * Set userPlatform 
    * 
    * @param \AppBundle\Entity\UserPlatform $userPlatform 
    * 
    * @return Question 
    */ 
    public function setUserPlatform(\AppBundle\Entity\UserPlatform $userPlatform = null) 
    { 
     $this->userPlatform = $userPlatform; 

     return $this; 
    } 

    /** 
    * Get userPlatform 
    * 
    * @return \AppBundle\Entity\UserPlatform 
    */ 
    public function getUserPlatform() 
    { 
     return $this->userPlatform; 
    } 
} 

タグエンティティ:私はテーブルQUESTION_TAGに値を入れしようとすると、

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Tag 
* 
* @ORM\Table(name="tag") 
* @ORM\Entity 
*/ 
class Tag 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=20) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    */ 
    private $name = ''; 


    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return tag 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 
} 

私はこのエラーがあります:

An exception occurred while executing 'INSERT INTO tag (name) VALUES (?)' with params ["PYTHON"]: 

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'PYTHON' for key 'PRIMARY' 

なぜ?

値がデータベースに既に存在する場合は入力しないでください。これを明確にする必要がある場合、どうすればよいですか?

は、あなたが彼らの仕事はあなたのケースでは、あなたのタグエンティティに次の制約を使用することができますので、制約に基づいてオブジェクトを検証するためにあるバリと呼ばれるこの事を持っているSymfony2のではあなたに

答えて

0

に感謝

http://symfony.com/doc/current/reference/constraints/UniqueEntity.html

そして、コントローラのバリデータサービスを呼び出します。クラスに対してFormTypeを使用すると、バリデータサービスでオブジェクトを自動的に検証する場合、この手順を回避できます。

http://symfony.com/doc/current/book/forms.html

実際にあなたがデータ構造を検証するためにフォームを使用することができます..しかし、それはofftopic

です.....

あなたがこれを行うことができ、他の方法は、タグを選択することです挿入して、結果が残っていないかどうかを確認したい場合は、タグが存在する場合はデータベースから取り除き、既存のタグを使用することができます。

0

問題は、会ったDBにデータを入れてHODので、このコード:問題はあなたがマネージャを介して値を取らなければならない関係に値を挿入するということでした

//the question with Id 1 exist already 
$question = $this->getDoctrine()->getManager()->getReference('AppBundle:Question',"1"); 

//the tag with name PYTHON exist already 
$tag = $this->getDoctrine()->getManager()->getReference('AppBundle:Tag',"PYTHON"); 

$questionTag = new QuestionTag(); 
$questionTag->setQuestion($question); 
$questionTag->setTag($tag); 

//now I call a service to put the item into DB 
$questionTagPF = $this->get('facade.QuestionTagFacade'); 
$res = $questionTagPF->create($questionTag); 

//the question with Id 1 exist already 
$question = new Question(); 
$question->setId(1); 

//the tag with name PYTHON exist already 
$tag = new Tag(); 
$tag->setName("PYTHON"); 

$questionTag = new QuestionTag(); 
$questionTag->setQuestion($question); 
$questionTag->setTag($tag); 

//now I call a service to put the item into DB 
$questionTagPF = $this->get('facade.QuestionTagFacade'); 
$res = $questionTagPF->create($questionTag); 

そう書かれなければなら!

関連する問題