fpn / doctrine-extensions-taggable
基于 Doctrine ORM 的标签系统
v0.9.0
2012-09-20 19:58 UTC
Requires
- doctrine/orm: >=2.0
This package is not auto-updated.
Last update: 2024-09-14 14:03:42 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
最后,您需要设置 doctrine 以注册元数据目录并注册 TagListener。
首先,注册此包的元数据目录。
$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); }