所以我目前正在尝试使用Symfony2和Doctrine进行简单的搜索。 与此类似的东西: http : //docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/searching.html
我目前有以下YAML
文件设置来生成我的实体。 它正确地将我的class Style
实体生成为一个类。
...\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中支持的功能。 您正在查看Symfony 1.x文档,Symfony2与Symfony 1.x真的不同,所以为了参考,您应该始终使用doc 。
有几种方法可以在Symfony2中获取实体。 这里有一些例子:
找
$user = $this->getDoctrine() ->getRepository('UserBundle:User') ->find($user_id) ;
DQL:
$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的Doctrine指南: http ://symfony.com/doc/current/book/doctrine.html
你好,你可以在symfony 3中做到这一点
$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();