违反完整性约束:1062重复输入'便携式工具'关键' UNIQ_63B58042B36786B'"

时间:2016-08-08 11:56:44

标签: php symfony orm doctrine

我试图在给定的表格中插入一些数据,我使用实体存储库来定义一个创建实体的函数(如果它不存在),找到它如果它存在。 我的实体文件如下:

             /**
      * SemanticTag
      *
     * @ORM\Table(name="Semantictag")
    *  @ORM\Entity(repositoryClass="VCycle\SemanticTagsBundle\Repository\SemanticTagRepository")
    */
    class SemanticTag
  {
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255, unique=true)
 */
private $title;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

public function __construct()
{
    $this->createdAt = new \DateTime();
}

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

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

    return $this;
}

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

public function getNormalizedTitle()
{
    return mb_strtolower($this->title);
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return $this
 */
public function setCreatedAt(\DateTime $createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

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

我的repositoty文件如下:

      /**
 * SemanticTagRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
  class SemanticTagRepository extends EntityRepository
 {
/**
 * @param array $titles
 *
 * @return SemanticTag[]
 */
public function findOrCreateByTitles(array $titles)
{
    $Semantictags = $this->findBy(array('title' => $titles));
    /* @var $tags Tag[] */

    $SemantictagsCollection = array();
    foreach ($Semantictags as $Semantictag) {
        $SemantictagsCollection[$Semantictag->getNormalizedTitle()] = $Semantictag;
    }

    $normalizedTitles = array();
    foreach ($titles as $title) {
        $normalizedTitles[mb_strtolower($title)] = $title;
    }

    $SemantictagsToCreate = array_diff($normalizedTitles, array_keys($SemantictagsCollection));

    foreach ($SemantictagsToCreate as $title) {
        $Semantictag = new SemanticTag();
        $Semantictag->setTitle($title);
        $this->_em->persist($Semantictag);

        $SemantictagsCollection[$Semantictag->getNormalizedTitle()] = $Semantictag;
    }

    return $SemantictagsCollection;
}
}

我尝试过:

        php app/console doctrine:cache:clear-metadata
        php app/console cache:clear

但它总是让我

            "message": "An exception occurred while executing 'INSERT INTO Semantictag (title, created_at) VALUES (?, ?)' with params [\"Portable tools\", \"2016-08-08 13:48:13\"]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Portable tools' for key 'UNIQ_63B58042B36786B'"

另一个实体如下

               /**
   * Tag_Semantictag
   *
   * @ORM\Table(name="tag_semantictag",
   *     uniqueConstraints={
   *     @ORM\UniqueConstraint(name="UNIQ_tag_semantictag", columns= {"tag_id", "semantic_tag_id"})
   * })
    *  @ORM\Entity(repositoryClass="VCycle\TagsBundle\Repository\TagSemantictagRepository")
     */
   class TagSemantictag
   {
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="Tag", inversedBy="TagSemantictags")
 * @ORM\JoinColumn(name="tag_id", nullable=false, onDelete="CASCADE")
 *
 * @var Tag
 */
private $tag;

/**
 * @ORM\ManyToOne(targetEntity="VCycle\SemanticTagsBundle\Entity\SemanticTag")
 * @ORM\JoinColumn(name="semantic_tag_id", nullable=false, onDelete="CASCADE")
 *
 * @var SemanticTag
 */
private $semantictag;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

public function __construct()
{
    $this->createdAt = new \DateTime();
}

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

function getTag() {
    return $this->tag;
}

function getSemantictag() {
    return $this->semantictag;
}

function setTag(Tag $tag) {
    $this->tag = $tag;
}

function setSemantictag(SemanticTag $semantictag) {
    $this->semantictag = $semantictag;
}


/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return Tag_SemanticTag
 */
public function setCreatedAt(\DateTime $createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

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

}

1 个答案:

答案 0 :(得分:0)

您正在使用已存在于数据库中的相同列组合执行另一个插入。一些现有记录具有Portable tools标题。您需要为要插入的每个实体创建唯一标题。