, , , , , . "--", .
: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/faq.html#why-do-i-get-exceptions-about-unique-constraint-failures-during-em-flush
:
public function addTag(Tag $tags)
{
if (!$this->tags->contains($tags))
$this->tags[] = $tags;
}
, , indexedBy = "name" fetch = "EXTRA_LAZY" , ( , ):
class Article
{
/**
* @ManyToMany(targetEntity="Tags", inversedBy="articles", cascade={"persist"}, indexedBy="name" fetch="EXTRA_LAZY")
*/
private $tags;
fetch = "EXTRA_LAZY" : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html
indexBy = "name" , Google "Doctrine + indexed association" , Doctrine docs ( , ).
addTag() :
public function addTag(Tag $tags)
{
if ($this->tags->contains($tags)) {
return $this;
}
$tagKey = $tag->getName() ?? $tag->getHash();
$this->tags[$tagKey] = $tags;
}
. PHP7 +.
EXTRA_LAZY, Doctrine SQL-, , (. EXTRA_LAZY ):
$this->tags->contains($tags)
.. true, PRIMARY KEY . Doctrine / PRIMARY KEY , ArrayCollection:: contains(). name - UNIQUE KEY, , false. PRIMARY KEY, , contains().
addTag() if ArrayCollection of Tags PRIMARY KEY , null) Tag ( Google "PHP + spl_object_hash", Doctrine ). , , , , .