PHP code example of anh / doctrine-extensions-taggable

1. Go to this page and download the library: Download anh/doctrine-extensions-taggable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anh / doctrine-extensions-taggable example snippets




use Anh\Taggable\TaggableInterface;
use Anh\Taggable\AbstractTaggable;

class Article extends AbstractTaggable implements TaggableInterface
{
    // ...

    public function getTaggableType()
    {
        return 'article';
    }
}



use Anh\Taggable\TaggableManager;
use Anh\Taggable\TaggableSubscriber;

// create entity manager
// $em = EntityManager::create(...);

// create taggable manager
$taggableManager = new TaggableManager(
    $em, 'Anh\Taggable\Entity\Tag', 'Anh\Taggable\Entity\Tagging'
);

// add event subscriber
$em->getEventManager()->addEventSubscriber(
    new TaggableSubscriber($taggableManager)
);

// create and fill entity
$article = new Article();
// $article->setTitle(...);

// add tag
$tag = $taggableManager->loadOrCreateTag('This is a tag');
$article->addTag($tag);

// or add multiple tags
$tags = $taggableManager->loadOrCreateTags(array('tag1', 'tag2', 'tag3'));
$article->addTags($tags);

// see Anh\Taggable\AbstractTaggable for more

$em->persist($article);
$em->flush();

// ...

// getting tagged resources
$repository = $taggableManager->getTaggingRepository();

$tag = $taggableManager->loadOrCreateTag('Some tag')
// returns all resources tagged with tag 'Some tag'
$resources = $repository->getResourcesWithTag($tag);
// returns only articles with tag 'Some tag'
$articles = $repository->getResourcesWithTypeAndTag('article', $tag);