0

I am using LIKE query in DOCTRINE1 to search keywords in DB. Here is the example : I have a string available in DB "Developer in the hell"

$str = "Developer in the hell";
$srchKey = "%" . mysql_real_escape_string($searchString) . "%";
$q = Doctrine_Query::create()->from("Movie as m")
                             ->where('m.name LIKE '."'$srchKey'". ' or m.keywords LIKE'."'$srchKey'")
                             ->fetchArray();

The concern is if I search for "Developer" it returns me the result but if I search for "Developer hell" is returns me nothing. Because middle to words from the string are skipped.

Is there any wild card/advance options/conditions that DOCTRINE provide to handle such case.

j0k
  • 22,600
  • 28
  • 79
  • 90
Amritpal singh
  • 855
  • 5
  • 12
  • 27
  • 1
    Doctrine escapes the parameters automatically, so you shouldn't worry...!! And, in addition, you shouldn't use `mysql_***` functions... Use `mysqli_***` instead, where **i** denotes **improved**. See **[the docs](http://www.php.net/manual/en/book.mysqli.php)** for more info. And the one you need right now is [this one](http://www.php.net/manual/en/mysqli.real-escape-string.php) – Nico Sep 22 '13 at 20:35

1 Answers1

1

First of all, you're doing it wrong when building the query.

Doctrine escape parameter that you give to the query, so you don't have to use mysql_real_escape_string on your own.

$str = "Developer in the hell";
$srchKey = "%" . $str . "%";
$q = Doctrine_Query::create()
    ->from("Movie as m")
    ->where('m.name LIKE ? or m.keywords LIKE ?', array($srchKey, $srchKey))
    ->fetchArray();

About your search engine, you should have a look at the Searchable behavior. It will be more powerfull than a simple LIKE query. But I don't know if it handle multiple term search like you want.

Or you can give a try to Lucene. A tutorial is available as part of the Jobeet tutorials.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • @jok Thanks for the correction. I have tried SEARCHABLE BEHAVIOR but no successes, now seems I have to go with Zend_Lucene. – Amritpal singh Jul 30 '13 at 09:30