【问题标题】:How to perform a search on several entities with Symfony 2如何使用 Symfony 2 对多个实体执行搜索
【发布时间】:2013-05-06 16:12:27
【问题描述】:
我需要对具有相同字符串的多个实体进行搜索,然后对结果进行排序。
我听说过/读过一些关于FOSElasticaBundle 的信息,这个捆绑包能做到吗?似乎(对我而言)为此目的几乎有很多功能,我不确定它是否可以在共享服务器(hostgator)上运行。
目前我能想到的另一个解决方案是“手动”搜索(通过使用 join 和 union),但我想知道我应该在哪里放置这样的功能:在现有控制器中,新的,新的捆绑包还是其他地方?
我也担心这种手动解决方案可能会产生成本,尤其是在某些不可索引的字段上。
【问题讨论】:
标签:
search
symfony
elasticsearch
【解决方案1】:
您将创建自定义实体存储库。查看the docs。基本上这扩展了默认的 FindAll、FindOneBy 等。
你会有这样的功能:
class MyEntityRepository extends Doctrine\ORM\EntityRepository {
public function findByCustomRule(){
//this is mapped to your entity (automatically adds the select)
$queryBuilder = $this->createQueryBuilder('someAlias');
$queryBuilder->orderBy('...');
//this is mapped to any entity
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$queryBuilder->select('...');
//result
$result = $queryBuilder->getQuery()->getResult();
}
}
这个类是在教义映射中定义的,并且位于 Entity 文件夹中。查看文档,您应该会有一个基本的概念。