CakePHP MySQL Search

What is the โ€œbestโ€ text search implementation for Cakephp using a MySQL database?

+7
php cakephp
source share
3 answers

How I implemented message search:

Search Form Code:

<?php echo $form->create('Deal',array('action' => 'search')); echo $form->input('Deal.search'); echo $form->end('Search'); ?> 

In the controller, enter the following search function:

 function search() { if (!empty($this->data)) { $searchstr = $this->data['Post']['search']; $this->set('searchstring', $this->data['Post']['search']); $conditions = array( 'conditions' => array( 'or' => array( "Post.title LIKE" => "%$searchstr%", "Post.description LIKE" => "%$searchstr%" ) ) ); $this->set('posts', $this->Post->find('all', $conditions)); } } 

View Code:

 <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['Post']['id']; ?></td> <td> <?php echo $html->link($post['Post']['title'],'/posts/view/'.$post['Post']['id']);?> </td> <td><?php echo $post['Post']['created']; ?></td> </tr> <?php endforeach; ?> 

It may not be the best / elegant, but works well for simple queries.

+7
source share

Sphinx is one of the most powerful SQL text search engines - http://sphinxsearch.com/

CakePHP behavior is written in the bakery: http://bakery.cakephp.org/articles/view/sphinx-behavior

It should be noted that Sphinx has several components, and some should run as daemons on your computer (similar to running apache or mysql processes). You also need to โ€œindexโ€ your database as often as possible to get fresh results. This can be a daunting task at first, but it is definitely worth the effort if you have many entries and large chunks of text to search for.

+1
source share

I have always sought to create a Search, SearchesController and view model, and then write my own script to search my database using

 $this->Search->query("SELECT * FROM table WHERE condition 1 AND condition 2"); 

I donโ€™t know a single โ€œbetterโ€ search, but there may well be components and plugins that will do something similar for you. The best way to check out baked goods is http://bakery.cakephp.org/

0
source share

All Articles