BeelabTagBundle
BeelabTagBundle copied to clipboard
Programmaticaly tags addition
Hi! Nice and clear bundle, thank you for your work! I have problem/proposition: I need to add/remove tags from my services. Now, bundle only allow to add tags via listener. What do you think, @garak it will be better, if we can have some service that manages tags and its relations? Inside listener we can call such service and avoid copypasting. If you agreed my idea, I can try to PR it.
I'm not sure to understand your use case. You can add/remove tags without using bundle's functionality, just relying on normal Doctrine entities relations.
I has the same problem, here is how I solved it: I first get my tag entity I want to add, then I try to save the entity:
$tag = $this->getDoctrine()->getRepository(Tag::class)->find($id);
if(!$entity->hasTag($destinationTag)) {
$entity->addTag($destinationTag);
}
// THIS NEXT LINE IS MANDATORY, DOESN'T WORK IF THE FIELD "UPDATED" IS NOT UPDATED
$entity->setTagsText($entity->getTagsText());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entity);
$entityManager->flush();
I am talking about this field in my entity:
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updated;
That is updated with the function in the same entity:
/**
* @param string
*/
public function setTagsText($tagsText)
{
$this->tagsText = $tagsText;
$this->updated = new \DateTime();
}
I guess it's just not very user friendly to use 2 functions to update 1 tag, but maybe there is another way ?
@antoine-nedelec what about calling $this->updated = new \DateTime();
inside addTag
mehod?