Trying to automatically persist tags

This commit is contained in:
Alejandro Celaya 2019-10-26 09:01:51 +02:00
parent f4e9d0c8fe
commit 1937f3ea22
2 changed files with 9 additions and 9 deletions

View File

@ -60,6 +60,7 @@ $builder->createManyToMany('tags', Entity\Tag::class)
->setJoinTable('short_urls_in_tags')
->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE')
->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE')
->cascadePersist()
->build();
$builder->createManyToOne('domain', Entity\Domain::class)

View File

@ -24,21 +24,20 @@ trait TagManagerTrait
$entities = [];
foreach ($tags as $tagName) {
$tagName = $this->normalizeTagName($tagName);
$tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?: new Tag($tagName);
$em->persist($tag);
$tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?? new Tag($tagName);
// if (! $tag) {
// $tag = ;
// $em->persist($tag);
// }
$entities[] = $tag;
}
return new Collections\ArrayCollection($entities);
}
/**
* Tag names are trimmed, lower cased and spaces are replaced by dashes
*
* @param string $tagName
* @return string
*/
private function normalizeTagName($tagName): string
private function normalizeTagName(string $tagName): string
{
return str_replace(' ', '-', strtolower(trim($tagName)));
}