使用Doctrine和Symfony2进行数据库搜索
因此,我目前正在尝试使用Symfony2和doctor执行一个简单的搜索。类似于此: 我目前有以下使用Doctrine和Symfony2进行数据库搜索,symfony,doctrine,Symfony,Doctrine,因此,我目前正在尝试使用Symfony2和doctor执行一个简单的搜索。类似于此: 我目前有以下YAML文件设置来生成我的实体。它将正确地生成我的类样式实体作为一个类 ...\Style: type: entity table: styles id: id: type: integer generator: strategy: IDENTITY actAs:
YAML
文件设置来生成我的实体。它将正确地生成我的类样式
实体作为一个类
...\Style:
type: entity
table: styles
id:
id:
type: integer
generator:
strategy: IDENTITY
actAs:
Searchable:
fields: [title]
batchUpdates: true
fields:
title:
type: string
length: 150
unique: true
在我的控制器中,我试图基于字符串在该表上运行搜索
public function searchAction($pattern)
{
$repository = $this->getDoctrine()->getRepository('..:Style');
$search = $repository->search($pattern);
return $this->outputize($search);
}
然而,当我尝试执行代码时,我得到以下异常
Undefined method 'search'. The method name must start with either findBy or findOneBy!
我是否正确生成实体,或者是否有明显遗漏的内容
另一方面,当我在生成后查看我的Entity/Style.php
时,没有明确的方法->search()
,这个函数应该由Symfony生成吗?search()
在Symfony2中不受支持。您正在查看symfony1.x文档,而Symfony2与symfony1.x确实不同,因此作为参考,您应该始终使用
在Symfony2中有几种获取实体的方法。以下是几个例子:
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->find($user_id)
;
$query = $em->createQuery(
'SELECT b FROM YourBundle:Bid b WHERE b.property_id = :property_id ORDER BY b.amount DESC'
)->setParameter('property_id', $property_id);
try {
$bids = $query->getResult();
} catch (\Doctrine\Orm\NoResultException $e) {
//Handle No Result Exception here
}
请参阅Symfony2的条令指南:您好,您可以在Symfony3中执行此操作
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Hotel p
WHERE p.address like :location
ORDER BY p.address ASC'
)->setParameter('location','%'.$request->get('location').'%' );
$hotel = $query->getResult();
请注意,symfony 2中使用的默认doctrine版本是2.x,并且通过执行
$this->getDoctrine()
所得到的可能是doctrine 2类而不是doctrine 1类…我正在尝试执行搜索,类似于mysql全文搜索功能。我也有同样的问题。我想使用WHERE子句来搜索LONGTEXT/BLOB字段,但我不能为它添加索引。我也不可能添加一些Apache插件,比如Lucene。