如何在symfony3中使用findBy?

时间:2016-04-06 15:34:39

标签: symfony

我正在使用symfony3: 我想在我的查询中使用。

 $repository = $this->getDoctrine()->getRepository('AppBundle:ABC'); 
       echo $searchstring = "LIKE '%".$searchtxt."%'"; die;
       $res=$repository->findBy(array('fname' => $searchstring));          
       $normalizer = new ObjectNormalizer();      
       $encoder = new JsonEncoder();
       $serializer = new Serializer(array($normalizer), array($encoder));
       $response=$serializer->serialize($res, 'json'); // Output: {"name":"foo","sportsman":false}
       return new Response($response);

返回 null

任何人都可以建议我吗?

1 个答案:

答案 0 :(得分:2)

Doctrine资源库的find*()方法不允许使用LIKE执行查询以进行比较。您需要编写自定义DQL查询(请参阅Querying for Objects with DQLDoctrine Query Language):

$query = $em->createQuery("SELECT abc FROM AppBundle:ABC abc WHERE fname LIKE :fname");
$query->setParameter('fname', 'LIKE "%'.$searchtxt.'%"');
$result = $query->getResult();