2013-01-20 34 views
5

क्या कोई मुझे यह बता सकता है कि यह क्वेरी क्यों काम नहीं कर रही है? मैंने सिंगल और डबल कोट्स के बीच भी बदलने की कोशिश की।डॉक्ट्राइन के निर्माण विधि के साथ वाइल्डकार्ड का उपयोग

$em = $this->getDoctrine()->getManager(); 

$query = $em->createQuery('SELECT t FROM AcmeBlogBundle:BlogTag t WHERE t.title LIKE \'%:title%\'') 
->setParameter('title', $keyword); 

सिद्धांत बस Invalid parameter number: number of bound variables does not match number of tokens देता है।

साथ ही, createQuery विधि याQueryBuilder का उपयोग करके ऐसी क्वेरी निष्पादित करना बेहतर है?

+0

मुझे लगता है कि आप सभी टेबल का चयन करना चाहते हैं, इसके लिए आपको SELECT * ... का उपयोग करना होगा और टी नहीं चुनें। क्या वह था – eLRuLL

उत्तर

22

PDO treats both the keyword and the % wildcards as a single token. You cannot add the wildcards next to the placeholder. You must append them to the string when you bind the params

PHP दस्तावेज़ों पर this comment भी देखें।

इसलिए आपको निम्न कार्य करने की आवश्यकता होगी:

$qb = $em->createQueryBuilder(); 

$qb 
    ->select('tag') 
    ->from('AcmeBlogBundle:BlogTag', 'tag') 
    ->where($qb->expr()->like('tag.title', ':title')) 
    ->setParameter('title', '%' . $keyword . '%') 
; 

या

$query = $em->createQuery('SELECT t FROM AcmeBlogBundle:BlogTag t WHERE t.title LIKE :title'); 
$query->setParameter('title', '%' . $keyword . '%'); 

मैं क्वेरी बिल्डर का उपयोग करने के लिए है क्योंकि यह संरचना अच्छे और अपने बयान और अधिक maintainable बना रही है पसंद करते हैं

+1

धन्यवाद सर, यह पूरी तरह से काम किया। आपकी सहायता के लिए धन्यवाद. –