icomefromthenet / quicktag
基于Doctrine DBAL构建的标签库
dev-master
2013-07-15 01:57 UTC
Requires
- icomefromthenet/dbal-gateway: dev-master
- symfony/event-dispatcher: ~2.3
- symfony/validator: ~2.3
Requires (Dev)
- monolog/monolog: ~1
- silex/silex: dev-master
- symfony/browser-kit: dev-master
- symfony/dom-crawler: dev-master
This package is not auto-updated.
Last update: 2024-09-14 15:01:48 UTC
README
#QuickTag - 为您的应用程序添加标签。
- 使用Doctrine DBAL编写。
- 可选的RESTful Silex API支持GET/POST/PUT/DELETE。
- 可与Zend/Tag/Cloud一起使用,创建标签云变得简单。
- PHP 5.3及更高版本。
####标签具有以下属性
- 标题(45个字符)名称。默认大小写无关。
- 权重(浮点数)用于对一组标签进行排序
- 创建日期(DateTime)用于对旧标签和新标签进行排序
- 用户上下文(整数)将标签限制为给定的用户ID。
##使用Composer安装
"require" : { "icomefromthenet/quicktag" : "dev-master", }
##运行
仅库本身。
$doctrine = new Doctrine\DBAL\Connection; $symfonyEventDispatcher = new Symfony\Component\EventDispatcher\EventDispatcher(); $monologBridge = new QuickTag\Log\MonologBridge($monolog); # bridget implements QuickTag\Log\LogInterface, write own bridge to change logger platform. $tableName = 'quicktag_tags'; # database table name $provider = new QuickTag\TagServiceProvider(); $tagService = $provider->instance($doctrine,$event,$logBridge,$tablename);
与Silex一起使用
在您app.php引导文件中。
####需要以下外部依赖
- Monolog
- Doctrine DBAL
- Symfony2事件调度器
# ---------------------------------------------------- # Include Composer Autoloader # # --------------------------------------------------- require_once(__DIR__ . "/vendor/autoload.php"); # ---------------------------------------------------- # Create the application # # --------------------------------------------------- $app = new Silex\Application(); #------------------------------------------------------------------ # Add Parse for json requests body # #------------------------------------------------------------------ $app->before(function (Symfony\Component\HttpFoundation\Request $request) { if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) { $data = json_decode($request->getContent(), true); $request->request->replace(is_array($data) ? $data : array()); } }); # ---------------------------------------------------- # Load ValidatorServiceProvider # # --------------------------------------------------- $app->register(new Silex\Provider\ValidatorServiceProvider()); # ---------------------------------------------------- # Setup Tags # # --------------------------------------------------- $app->register(new QuickTag\Silex\Provider\TagServiceProvider('qtag'), array( 'qtag.options' => array( 'tableName' => 'quicktag_tags' ) )); #------------------------------------------------------------------ # Setup Routes / Controllers # #------------------------------------------------------------------ $app->mount('/', new QuickTag\Silex\Controllers\TagProvider('qtag')); return $app;
如果您运行不同的实例,则需要更改索引和表名。在上面的示例中,索引设置为qtag。您应该使用应用程序的名称对索引进行命名空间。
###设置标签表的SQL
别忘了更改表名!
delimiter $$ CREATE TABLE `quicktag_tags` ( `tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `tag_user_context` int(10) unsigned DEFAULT NULL, `tag_date_created` datetime NOT NULL, `tag_weight` double DEFAULT NULL, `tag_title` varchar(45) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`tag_id`), KEY `IDX_FF11E9291A46B076` (`tag_user_context`) ) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$
API方法
存储标签:创建/更新
use DateTime; use QuickTag\Model\StoredTag; $storedTag = new StoredTag(); $storedTag->setTitle('mytitle'); $storedTag->setWeight(1); $storedTag->setTagCreated(new DateTime()); $storedTag->setUserContext(3); # fetch service from the provider $result = $tagService->storeTag($storeTag); if($result) { echo 'tag has been stored at id '. $storedTag->getTagId(); }
在更新期间,必须设置id,并且只能更改标题、权重和用户上下文。
通过id查找标签。
use QuickTag\Model\StoredTag; # fetch service from the provider $storeTag = $tagService->lookupTag($id); if($storedTag instanceOf StoredTag ) { echo 'tag has been gound at id '. $storedTag->getTagId(); }
搜索标签
# Search for tags started with titte `my` and belong to user 3 $tagCollection = $tagService->findTag() ->start() ->limit($limit) ->offset($offset) ->orderByTitle('asc') ->filterByNameStartsWith('my') ->filterByUserContext(3) ->end() ->find(); if(count($tagCollection) > 0 ) { echo sprintf('found %s number of tags',count($tagCollection)); }
删除标签。
use QuickTag\Model\StoredTag; $id = 1; # fetch service from the provider $storeTag = $tagService->lookupTag($id); $result = $tagService->removeTag($storeTag); if($result) { echo 'tag has been removed at id '. $storedTag->getTagId(); }
QuickTag\Silex\Provider\TagServiceProvider
下的API有如何使用库的基本示例。
使用Zend Tag Cloud
use Zend\Tag\Cloud; use QuickTag\Model\StoredTag; $tagA = new StoredTag(); $tagB = new StoredTag(); $tagC = new StoredTag(); $cloud = new Cloud(array( 'tags' => array( $tagA,$tagB,$tagC ) )); // Render the cloud echo $cloud;