score:3

Accepted answer

The problem you're facing is that you are creating new Tag entities for every post, so Doctrine tries to persist them and then the database complaints about your unique restriction.

Your insert code should look similar to:

$news = //get the NEWS to persist
foreach($tags as $tag)
{
    if (($persistedTag = $entityManager->getRepository('AppBundle:Tag')->findOneByName($tag))==null)
    {
        $persistedTag = new Tag();
        //Fill the new Tag entity
        $entityManager->persist($persistedTag);
    }
    $persistedTag->addNews($news);
    $news->addTag($persistedTag);
}
$entityManager->flush();

Related Query

More Query from same tag