Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I need examples of search code for Symfony 1.0 Symfony


I need code examples of search code. This is for Symfony 1.0. I have been writing a lot of LIKE statements, but I need something better than that. Any plugins suggestion welcome, but I'd really like to see actual samples of action code for handling stuff like multiple words, and how did folks handle weighting.

In general, how much do folks like to push these issue out to the database? How much do folks like to do using PHP?

My database is MySql, and in this case I do not mind dropping out of Propel, if Propel gets in the way of doing good searches.

Answers (9)


Arturo Linares answers:

You could try concatenating all columns you want to look into, and then use LIKE in the criteria. Something like this:

$term = 'test';
$c = new Criteria();

. CommentPeer::TITLE . " , '|' ,"
. CommentPeer::DESCRIPTIION . " , '|'"
. '',
. CommentPeer::TITLE . " , '|' ,"
. CommentPeer::DESCRIPTIION . " , '|'"
. ')'
. ' LIKE \'%'.$term.'%\'',

$rs = CommentPeer::doSelect($c);

* I updated the criteria, I was doing it backwards.

Arturo Linares comments:

If you use Lucene Eduardo has a good mini tutorial here :)

But in sf1.0 you doesn't have ProjectConfiguration class. To autoload Zend libraries you'll have to add this to your settings.yml

zend_lib_dir: %SF_LIB_DIR%
- [sfZendFrameworkBridge, autoload]

The dir structure in /lib would be:


marshall comments:

Arturo, thanks much. I think some combination of what you posted and what Bill Hunt posted is going to be our path.


Eduardo Raffoul answers:

Zend Lucene has a great tool to perform searchs.

This is what i've done (based on Jobeet tutorial, and has worked great):

In the peer method declare
static public function getLuceneIndex()

if (file_exists($index = self::getLuceneIndexFile()))
return Zend_Search_Lucene::open($index);
return Zend_Search_Lucene::create($index);

static public function getLuceneIndexFile()
return sfConfig::get('sf_data_dir').'/person.'.sfConfig::get('sf_environment').'.index';

Each time you save/update/delete a record, must update
public function save(PropelPDO $con = null)
$object = parent::save($con);


return $object;

Still in your model, if you delete a field, you have to delete it from your search results

public function delete(PropelPDO $con = null)
$luceneIndex = PersonPeer::getLuceneIndex();

foreach ($luceneIndex->find('pk:'.$this->getId()) as $occurrence)

return parent::delete($con);

Inside your class, you have to define what is gonna be indexed

public function updateLuceneIndex()
$luceneIndex = JobeetJobPeer::getLuceneIndex();

// remove existing entries (according to the id)
foreach ($luceneIndex->find('pk:'.$this->getId()) as $occurrence)

$doc = new Zend_Search_Lucene_Document();

// store job primary key to identify it in the search results
$doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));

// Here you must add the fields you want to index
$doc->addField(Zend_Search_Lucene_Field::UnStored('name', $this->getName(), 'utf-8'));
$doc->addField(Zend_Search_Lucene_Field::UnStored('lastName', $this->getLastName(), 'utf-8'));
$doc->addField(Zend_Search_Lucene_Field::UnStored('passport', $this->getPassport(),'utf-8));
// For any other fields of your interest, have to do the same

// add job to the index

To implement the search, in your action

class personActions extends sfActions

public function executeSearch(sfWebRequest $request)
if (!$query = $request->getParameter('query'))
return $this->forward('person', 'index');

$this->jobs = PersonPeer::getForLuceneQuery($query);

// .......


In your peer method, this is getForLucenQuery

static public function getForLuceneQuery($query)
$occurrences = self::getLuceneIndex()->find($query);

$pks = array();
foreach ($occurrences as $occurrence)
$pks[] = $occurrence->pk;

$criteria = new Criteria();
$criteria->add(self::ID, $pks, Criteria::IN);

return self::doSelect($criteria);

And the rest is to implement the view that is just to show your results

I didn't told you the most impotant yet! If you don't know how to install it. I see according to other answers that there is a plugin for zend lucene search. But, what i have done is this:
Download the framework from
I have it saved on /lib/vendor/Zend

And in your config/ProjectConfiguration.class.php add this

static protected $zendLoaded = false;

static public function registerZend()
if (self::$zendLoaded)

self::$zendLoaded = true;

The difference with the plugin solution is that you wont need to load the ProjectConfiguration::registerZend() method and i guess that the object Zend_Search_Lucene_Document may have another name according to symfony's name's structure

------ UPDATE -------
The piece of code above doesn't applies to symfony 1.0 as Arturo Linares remarks on his answer. BTW, i was checking the sfLucenePlugin and i see that it's still on beta status and hasn't been updated since April 08. I've never used this plugin, so i can't recommend to use it or not, instead my advice would be (if you decide to go on this way) to look around what people say about it, just to be sure that it's safe to use it in a production environment.

marshall comments:

Eduardo, thanks. After some thought, we decided to postpone Lucene. We plan to implement this long-term, but in the short term we are going to work with MySql full text searches, as Bill Hunt suggested. It is faster to implement.


Ender Technology answers:

[[LINK href=""]]sfLucenePlugin[[/LINK]] is going to be your fastest and easiest route to improving the search in regards to speed, keywords, weighting and ranking.

The sfLucenePlugin README and the included app should be plenty to get you started out on it. If you need to start tweaking it or customizing it, I suggest checking out the excellent docs at: [[LINK href=""]][[/LINK]]

Beware of adding wildcards to both sides of your keywords because that can result in significant slowdown and usually doesn't give the correct results users are looking for anyways.

Rob O.
Ender Tech Corp.
[[LINK href=""]][[/LINK]]

Ender Technology comments:

I forgot to mention that sfLucenePlugin is going to be the most portable too. The Lucene indexer and query is all implemented in PHP with no further dependencies. Any other solution is going to require you to spend time compiling/installing server software or concerning yourself with MyISAM.

However, being implemented in PHP this can also work against you if speed is really a concern but with sfLucene being easy to install, index, and test then it shouldn't be difficult to establish a benchmark to make decisions against.


Jakub Zalas answers:

If you have a lot of data or need a serious, more advanced and sophisticated search engine than you should use Apache Solr. Solr is an extension for the original Lucene (the one written in java).

<strong>If you don't need serious search option or don't work on a high traffic website than just go for Zend Lucene (PHP).</strong>

With Solr Basically you push your search out of the database and PHP. Problem in your case would be that you're using no longer supported symfony version (you should think about the upgrade) and there's no plugin for it. However, you can handle it on your own or use existing PHP library. Maybe you could also adapt one of the existing plugins for newer symfony versions.

To make your application working with Solr you need to configure it first. You need to tell Solr what is going to be indexed and how by preparing special xml file.

Later you need to feed the index. Your data needs to be pushed to solr server.

While searching you should connect to solr server. In response it gives you the list of ids which match the criteria. You can use these ids to get the full information out of the database and present it to the user.

<strong>It's extremely fast.</strong>

[[LINK href=""]]Read more about Solr features[[/LINK]]
[[LINK href=""]]Solr and PHP[[/LINK]]
[[LINK href=""]]Solr tutorial[[/LINK]]
[[LINK href=""]]Enterprise search with PHP and Apache Solr[[/LINK]]

<strong>Sphinx</strong> is an alternative and luckily for you there is a symfony 1.0 plugin: [[LINK href=" "]]sfSphinxPlugin[[/LINK]]

Sphinx might be a simpler solution to implement.

marshall comments:

Looking around, I find out that other departments in my company are using Sphinx. But I am told it is too big for what we are trying to do.


Bill Hunt answers:

Halfway between Lucene and LIKE, there's Full Text Search in MySQL. If you're looking for for something with more power and without having to implement a serious solution, it's the way to go. [[LINK href=""]]Here's the documentation.[[/LINK]] In summary, you add a new type of Index on the column or columns that you're going to be searching and then match text against them. It handles the tokenizing for you, and you can even get back a decimal value to tell you how close it matches. You use it like a standard where clause, [[LINK href=""]]there are a few examples here[[/LINK]].

marshall comments:

Thanks for the link to the examples. Very useful. We are going to do some of this.


Dragos Mihai Rusu answers:

Another approach would be using [[LINK href=""]]Sphinx[[/LINK]] and [[LINK href=""]]sfSphinxPlugin[[/LINK]].
Basically it's another application that runs and collects data from MySQL ( an SQL query is built to index data you need ).
It's very fast, and you can keep propel.
EDIT: One of Sphinx's advantages would be to get search results by relevance. I know people who implemented Sphinx for this feature only.

You will have to modify the application (search component): first query sphinx to get ids and afterwards query database to get data based on PK (which is pretty fast).
EDIT: searching *word* - wildcards can be enabled also

The bottleneck would be when you have a lot of updated information in each minute and you need all updates in search almost instantly. Solution to this would be implementing 2 merged indexes (1 large one, 1 daily) and run 2 crons: 1 daily at least accessed hours to update the large one, and one at 2-3-5 minutes which will only update info in the current day.

I can provide more information (at no cost) ( [email protected] ).

marshall comments:

Dragos, thanks. I am just told we use Sphinx elsewhere in company. When the rest of the company switches to Symfony, it will be good to use the plugin.


LuĆ£ de Souza answers:

You can try sfLucene plugin,


Martin Palacio answers:

Hi Marshall,

you can give a look at the excellent Askeet tutorial (it's for sf 1.0)

here you have a full example: [[LINK href=""]][[/LINK]]

good luck!

EDIT: as the tutorial says, you may consider to try the Lucene approach.
This is a great tutorial on how to use it, however, is not available for sf 1.0:
[[LINK href=""]][[/LINK]]

...maybe you can adapt it to your needs.

Moreover, looking at the date of this post, it seems to correspond to sf 1.0:
[[LINK href=""]][[/LINK]]

Remember, you can install the Lucene library directly from the Zend Framework, or by using the sfLucenePlugin. BTW the readme offers a good kickstart: [[LINK href=""]][[/LINK]]


sandip limbachiya answers:


You can define option in dropdown in template files and than selected value you can get into actions. than put below code in your action.

$omCriteria = new Criteria();

$this->ssSearchValue = trim($this->getRequestParameter('searchvalue' , ''));

$this->ssSearchBy = strtoupper($this->getRequestParameter('searchby'));


$rs = CommunityPeer::doSelect($omCriteria);

I think, this would be helpful to you other below are another link which is also helpful please check below link.

Sandip Limbachiya