php - Integrity constraint violation: 1062 Duplicate entry 'Portable tools' for key 'UNIQ_63B58042B36786B'" -
i'm trying insert data in given table, i'm using entity repository define function that's create entity if it's not existing, of find if it's exist. entity file following:
/** * 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(); } /** * 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; } /** * 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; } /** * createdat * * @return \datetime */ public function getcreatedat() { return $this->createdat; } }
my repositoty file follows:
/** * semantictagrepository * * class generated doctrine orm. add 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 $semantictag) { $semantictagscollection[$semantictag->getnormalizedtitle()] = $semantictag; } $normalizedtitles = array(); foreach ($titles $title) { $normalizedtitles[mb_strtolower($title)] = $title; } $semantictagstocreate = array_diff($normalizedtitles, array_keys($semantictagscollection)); foreach ($semantictagstocreate $title) { $semantictag = new semantictag(); $semantictag->settitle($title); $this->_em->persist($semantictag); $semantictagscollection[$semantictag->getnormalizedtitle()] = $semantictag; } return $semantictagscollection; } }
i tried with:
php app/console doctrine:cache:clear-metadata php app/console cache:clear
but gives me
"message": "an exception occurred while executing 'insert semantictag (title, created_at) values (?, ?)' params [\"portable tools\", \"2016-08-08 13:48:13\"]:\n\nsqlstate[23000]: integrity constraint violation: 1062 duplicate entry 'portable tools' key 'uniq_63b58042b36786b'"
the other entity follows
/** * 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(); } /** * 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; } /** * createdat * * @return \datetime */ public function getcreatedat() { return $this->createdat; }
}
you doing insert same combination of columns in database. existing record has portable tools
title. need create unique title each entity want insert.
Comments
Post a Comment