emhar / search-doctrine-bundle
在小型网站上创建搜索服务的一种简单方法。它无需搜索引擎,只需使用 Doctrine ORM。
dev-master
2014-08-23 12:52 UTC
Requires
- doctrine/annotations: >=1.0.0
- doctrine/cache: >=1.0.0
- doctrine/orm: >=2.4.0
- symfony/form: >=2.3.0
- symfony/framework-bundle: >=2.1.8
Requires (Dev)
This package is not auto-updated.
Last update: 2020-01-06 03:17:24 UTC
README
什么是 SearchDoctrineBundle ?
在小型网站上创建搜索服务的一种简单方法。它无需搜索引擎,只需使用 Doctrine ORM。用简单的 PHP 类描述您的结果,并将其与 Doctrine 实体绑定。
此 Symfony2 Bundle 提供一个服务,可生成搜索表单、结果和页数。结果对象按分数排序,分数根据请求的词权重在命中中计算。
安装
Composer
将以下依赖项添加到您的项目 composer.json 文件中
"require": {
# ..
"emhar/search-doctrine-bundle": "dev-master"
# ..
}
您必须在您的 AppKernel.php 中启用此 Bundle
// app/AppKernel.php public function registerBundles() { return array( //... new Emhar\SearchDoctrineBundle\EmharSearchDoctrineBundle(), //... ); }
用法
描述一个搜索项
//... use Emhar\SearchDoctrineBundle\Item\AbstractItem; use Emhar\SearchDoctrineBundle\Mapping\Annotation\Hit; use Emhar\SearchDoctrineBundle\Mapping\Annotation\ItemEntity; /** * @ItemEntity( * identifier="artist", * label="My Artists", * entityClass="MyBundle\Entity\Artist" * ) * @ItemEntity( * identifier="album", * label="My Albums", * entityClass="MyBundle\Entity\Album" * ) */ class MyItem extends AbstractItem { //... /** * Construct Resource * * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID", mapping={ * "album"="id", * "artist"="id" * }) * @Hit(identifier="name", scoreFactor=3, sortable=false, label="Name", mapping={ * "album""name", * "artist""name" * }) */ public function __construct($id, $name, $score, $type) { //... } //... }
调用服务以获取表单、结果和页数
//... use ...\Resource; //... $searchService = $this->get('emhar_search_doctrine.search_service'); /* @var $searchService \Emhar\SearchDoctrineBundle\Services\SearchService */ $form = $searchService->getForm(MyItem::getClass(), $this->generateUrl('...')); $form->handleRequest($this->getRequest()); if ($form->isValid()) { $items = $searchService->getResults(MyItem::getClass(), $form, $page); $pageCount = $searchService->getPageCount(MyItem::getClass(), $form); } //...
您必须为
getForm
方法提供一个操作 URI
ItemEntity 注解
/** * @ItemEntity( * identifier="album", * label="My Albums", * entityClass="MyBundle\Entity\Album" * ) */
- identifier: 您选择的标识符
string
必需
- label: 实体标签,将在表单中使用
string
必需
- entityClass: 实体类,必须是一个有效的 doctrine 实体
string
必需
Hit 注解
/** * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID" mapping={ * "album"="id", * "artist"="id" * }) */
- identifier: 构造函数参数
string
必需
- scoreFactor: 分数中的命中因子
int
默认 1
- sortable: 布尔值,确定是否可排序命中
bool
默认 false
,目前不受支持 - label: 此命中的标签
string
必需
- 映射键: 从 SearchItem 注解的实体标识符
string
- 映射值: 实体属性名称
string
必需
几种类型,字符串转换
如果命中依赖于实体的类型,则根据类型返回
- 结果被转换为字符串
- 空字符串。
/** * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID", mapping={ * "album"="id", * "artist"="name" * }) */
省略某些实体
您可以在命中定义中省略某些实体,返回 null 值。
/** * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID", mapping={ * "album"="id", * "artist"="id" * }) * @Hit(identifier="name", scoreFactor=3, sortable=false, label="Name", mapping={ * "album"="name" * }) */
链式获取器,nToOne 关系
您可以链式获取 nToOne 关系的获取器,通过点连接属性名称。
/** * @Hit(identifier="ownerName", scoreFactor=2, sortable=false, label="Owner Name", mapping={ * "album"="artist.name", * "track"="album.artist.name" * }) */
警告:链式获取器在 nToMany 关系中重复结果。
获取分数和实体标识符
如果您给项目参数命名
- 得分:接收项目得分,根据与请求文本匹配的字符数计算
- 类型:接收实体标识符,用于识别结果来源,可能生成不同的URL...
//... /** * Construct Resource * * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID", mapping={ * "album"="id", * "artist"="id" * }) * @Hit(identifier="name", scoreFactor=3, sortable=false, label="Name", mapping={ * "album""name", * "artist""name" * }) */ public function __construct($id, $name, $score, $type) { //... } //...
如果不需要,可以省略它们
//... /** * Construct Resource * * @Hit(identifier="id", scoreFactor=0, sortable=false, label="ID", mapping={ * "album"="id", * "artist"="id" * }) * @Hit(identifier="name", scoreFactor=3, sortable=false, label="Name", mapping={ * "album""name", * "artist""name" * }) */ public function __construct($id, $name) { //... } //...