Php 使用Doctrine2 Symfony将元素放入数据库时出错

Php 使用Doctrine2 Symfony将元素放入数据库时出错,php,symfony,orm,doctrine-orm,Php,Symfony,Orm,Doctrine Orm,我试图在一个名为问号的表格中放入一个元素,以便: //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(); $questionTa

我试图在一个名为问号的表格中放入一个元素,以便:

//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;
    }
}
这些是实体类: 关系(疑问句和标记之间):

为什么?

如果数据库中已经存在值,则不应避免输入该值?如果说得很清楚,我该怎么办


谢谢

在Symfony2中,您有一个称为验证器的东西,它们的工作是基于约束验证对象,因此在您的情况下,您可以在标记实体中使用以下约束

然后调用控制器中的验证程序服务。如果对类使用FormTypes,则可以避免此步骤,因为它们会使用验证器服务自动验证对象

实际上,您可以使用这些表单来验证数据结构。。但这是离题的


另一种方法是选择要插入的标记,并检查是否有结果。如果结果不好,则可以插入标记(如果存在)。您只需从数据库中恢复标记并使用现有标记。问题在于我为将数据放入数据库的方法提供的参数,因此以下代码:

//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);
必须这样写:

//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);
问题是,要在关系中插入值,必须通过管理器获取值

<?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'
//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);
//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);