juanmf / doctrine-extensions-taggable
基于Doctrine ORM的标签系统
v0.9.1
2014-08-12 17:12 UTC
Requires
- doctrine/orm: >=2.0
This package is not auto-updated.
Last update: 2024-09-24 07:57:43 UTC
README
此存储库包含Doctrine 2的Taggable扩展。这允许您轻松地为您的doctrine实体添加标签。
使用
实现DoctrineExtensions\Taggable\Taggable接口。
首先,您的实体必须实现DoctrineExtensions\Taggable\Taggable接口。您的实体中必须编写三个方法
getTaggableType()必须返回一个唯一的实体模型名称getTaggableId()必须返回一个唯一的实体标识符getTags()必须返回一个doctrine集合(Doctrine\Common\Collections\Collection)
示例
namespace MyProject;
use DoctrineExtensions\Taggable\Taggable;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
*/
class Article implements Taggable
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
public $id;
/**
* @Column(name="title", type="string", length=50)
*/
public $title;
protected $tags;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
return $this->title = $title;
}
public function getTaggableType()
{
return 'article';
}
public function getTaggableId()
{
return $this->getId();
}
public function getTags()
{
$this->tags = $this->tags ?: new ArrayCollection();
return $this->tags;
}
}
配置Doctrine
最后,您需要为注册元数据目录和注册TagListener配置doctrine。
首先,注册此包的元数据目录。
$config = new \Doctrine\ORM\Configuration();
// ...
$driverImpl = new \Doctrine\ORM\Mapping\Driver\XmlDriver(array('/path/to/doctrine-extensions-taggable/metadata'));
$config->setMetadataDriverImpl($driverImpl);
或者使用DriverChain
$driverImpl = new \Doctrine\ORM\Mapping\Driver\DriverChain();
// ...
$driverImpl->addDriver(new \Doctrine\ORM\Mapping\Driver\XmlDriver('/path/to/doctrine-extensions-taggable/metadata'), 'DoctrineExtensions\\Taggable\\Entity');
然后,注册TagListener。
// $this->em = EntityManager::create($connection, $config);
// ...
$this->tagManager = new TagManager($this->em);
$this->em->getEventManager()->addEventSubscriber(new TagListener($this->tagManager));
使用TagManager
现在,您可以使用TagManager。
// Load or create a new tag
$tag = $this->tagManager->loadOrCreateTag('Smallville');
// Load or create a list of tags
$tagNames = $this->tagManager->splitTagNames('Clark Kent, Loïs Lane, Superman'));
$tags = $this->tagManager->loadOrCreateTags($tagNames);
// Add a tag on your taggable resource..
$this->tagManager->addTag($tag, $article);
// Add a list of tags on your taggable resource..
$this->tagManager->addTags($tags, $article);
// Remove a tog on your taggable resource..
$this->tagManager->remove($tag, $article);
// Save tagging..
// Note: $article must be saved in your database before (persist & flush)
$this->tagManager->saveTagging($article);
// Load tagging..
$this->tagManager->loadTagging($article);
// Replace all current tags..
$tags = $this->tagManager->loadOrCreateTags(array('Smallville', 'Superman'));
$this->tagManager->replaceTags($tags, $article);
与标签相关的查询
标签实体有一个仓库类,包含两个特别有用的方法
<?php // somewhere crate or already have the entity manager // $em = EntityManager::create($connection, $config); $tagRepo = $em->getRepository('DoctrineExtensions\\Taggable\\Entity\\Tag'); // find all article ids matching a particular query $ids = $tagRepo->getResourceIdsForTag('article_type', 'footag'); // get the tags and count for all articles $tags = $tagRepo->getTagsWithCountArray('article_type'); foreach ($tags as $name => $count) { echo sprintf('The tag "%s" matches "%s" articles', $name, $count); }