BeelabTagBundle icon indicating copy to clipboard operation
BeelabTagBundle copied to clipboard

Programmaticaly tags addition

Open sc0rp10 opened this issue 7 years ago • 3 comments

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.

sc0rp10 avatar Jan 09 '18 23:01 sc0rp10

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.

garak avatar Jan 10 '18 08:01 garak

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 avatar Dec 06 '18 15:12 antoine-nedelec

@antoine-nedelec what about calling $this->updated = new \DateTime(); inside addTag mehod?

garak avatar Dec 06 '18 16:12 garak