Search feature to CakePHP blog example

By Calin Don (calin)
Using searchable behavior, exemplified on CakePHP's blog example from documentation.

Hello everybody!

First of all I assume that you have read and tried the blog example form documentation http://book.cakephp.org/view/219/blog).

Setup

So to get things started, we need to download seachable behavior from (http://code.google.com/p/searchable-behaviour-for-cakephp). Then we copy the archive contents to the /app folder. To complete the installation process there is one more thing to do: create the search table. So we run the following sql code:

Download code
CREATE TABLE `search_index` (
    `id` int(11) NOT NULL auto_increment,
    `association_key` int(11) NOT NULL,
    `model` varchar(128) collate utf8_unicode_ci NOT NULL,
    `data` longtext collate utf8_unicode_ci NOT NULL,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    PRIMARY KEY  (`id`),
    KEY `association_key` (`association_key`,`model`),
    FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

So we are set up now, ready to add our nice search feature!

The actual search

First we need to add the searchable behavior to Post model.

Model Class:

Download code <?php 
<?php
// app/models/post.php
class Post extends AppModel
{
    var 
$name 'Post';
    var 
$actsAs = array ('Searchable');

    var 
$validate = array(
        
'title' => array(
            
'rule' => array('minLength'1)
        ),
        
'body' => array(
            
'rule' => array('minLength'1)
        )
    );
}
?>
?>

To be able to search, we need to place a search box somewhere. Add the following snippet of code just after the page heading.

Download code
<?php 
    
echo $form->create("Post",array('action' => 'search'));
    echo 
$form->input("q", array('label' => 'Search for'));
    echo 
$form->end("Search");
?>

We need to define the search action in our post controller.

Download code
<?php
    
function search() {
        
$this->set('results',$this->Post->search($this->data['Post']['q']));
    }
?>

Finally we create the view for search results (which is just a slightly modified version of posts index):

View Template:

Download code
<?php // app/views/posts/search.ctp ?>
<h1>Blog posts</h1>
<?php 
    
echo $form->create("Post",array('action' => 'search'));
    echo 
$form->input("q", array('label' => 'Search for'));
    echo 
$form->end("Search");
?>
<p><?php echo $html->link("Add Post""/posts/add"); ?>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
                <th>Action</th>
        <th>Created</th>
    </tr>

<!-- Here's where we loop through our $results array, printing out post info -->

<?php foreach ($results 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 $html->link(
                
'Delete'
                
"/posts/delete/{$post['Post']['id']}"
                
null
                
'Are you sure?'
            
)?>
            <?php echo $html->link('Edit''/posts/edit/'.$post['Post']['id']);?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
<?php endforeach; ?>
</table>

That's it! You now have a blog with a fully featured search engine.

For other ways of using the searchable behavior, check the project page on Google Code (http://code.google.com/p/searchable-behaviour-for-cakephp).

 

Comments 791

CakePHP Team Comments Author Comments
 

Comment

1 little bug

Hello,

You say that «By default all varchar, char and text fields are indexed.»
But this is not true because getColumnTypes will say varchar is 'string'

$columns = $this->model->getColumnTypes();

so just add

if (isset($columns[$key]) && in_array($columns[$key],array('string','text','varchar','char'))) {...}

Thanks for the behavior, great work :D
Posted Nov 12, 2008 by seb
 

Bug

2 a typo

In the code shown for the model class, you open and close the ?php tag twice.
Posted Nov 27, 2008 by Adriano Varoli Piazza
 

Comment

3 Thank you!

Thankyouthankyouthankyou! This is what I've been looking for all along!
Posted Nov 27, 2008 by Adriano Varoli Piazza
 

Comment

4 Extension request:

Something I'm struggling with is searching belongsTo relationships and returning the id of the object's parent, not of the actual object found.
Example:
I have a 'paintings' table (not the actual table/class name) that hasMany 'titles'. I'd like to search for one of the 'titles' (bonus points if that search can be done from the 'painting' views) and that the returned list showed the link to the actual 'painting', not to the 'title'.

I understand it's a lot to ask, so thanks in advance for _any_ help.
Posted Dec 9, 2008 by Adriano Varoli Piazza
 

Question

5 It doesn't search

Hello together,

i've tried it with my own application(a very very simple dictionary) I implemented directly to this docu...
But the searchreslut is always empty....

What i have to do that the result is correct.

my translation DB is

CREATE TABLE translates (
id int(10) unsigned NOT NULL auto_increment,
language1 varchar(255) NOT NULL,
language2 varchar(255) NOT NULL,
created datetime default NULL,
modified datetime default NULL,
PRIMARY KEY (id)
);

The search_index is the same...

Can erveryone help me. If i search then is no error message...

Thanks

Many greetings

Marcus Radisch
Posted Dec 17, 2008 by Marcus Radisch
 

Question

6 Whats wrong with this fulltext search

Hello

now i tried it with the official Blog Tutorial, but there is no way it doesnt search. Can every help me a little bit

many greetings

Marcus Radisch
Posted Dec 17, 2008 by Marcus Radisch
 

Comment

7 Re: Whats wrong with this fulltext search

Hello

now i tried it with the official Blog Tutorial, but there is no way it doesnt search. Can every help me a little bit

many greetings

Marcus Radisch

Try reading this link: http://code.google.com/p/searchable-behaviour-for-cakephp/issues/detail?id=2
Posted Dec 18, 2008 by Adriano Varoli Piazza
 

Question

8 Re: Whats wrong with this fulltext search

Hello

now i tried it with the official Blog Tutorial, but there is no way it doesnt search. Can every help me a little bit

many greetings

Marcus Radisch

Try reading this link: http://code.google.com/p/searchable-behaviour-for-cakephp/issues/detail?id=2

I tried ist, but there is no change is the result search engine doesn't search.

Whats the reason? I' don't know. Do you have any ideas?
Posted Dec 18, 2008 by Marcus Radisch
 

Bug

9 A german "umlaute" (äöüß) Bug

Hello

can everyone help me? I have added an Bug for this component under this link->http://code.google.com/p/searchable-behaviour-for-cakephp/issues/detail?id=9 Until this moment i don't found any solution

Many greeting

marcus radisch
Posted Dec 19, 2008 by Marcus Radisch
 

Question

10 How can i indexing my hole DB-Table

Hello,

thanks a lot for the great help. My Question is now, i have an DB-Table with 2 Columns. How can i great an searchindex over the Table?

many greatings

Marcus Radisch
Posted Dec 19, 2008 by Marcus Radisch
 

Comment

11 asking questions

Hello,

thanks a lot for the great help. My Question is now, i have an DB-Table with 2 Columns. How can i great an searchindex over the Table?

many greatings

Marcus Radisch

I understand you're not familiar with English, and that's not a problem. What is a problem is that you don't give us enough information to help you.
If you say 'I have a table with two columns, how can I get a searchindex', I'll answer 'read the tutorial and follow the instructions'. After all, they worked for me. Please say in detail what you've done, where you got stuck. 'It doesn't work' is not a good report.

Cheers
Adriano.
Posted Dec 19, 2008 by Adriano Varoli Piazza
 

Comment

12 Input Field side by side

Hello

i've an little Question
I tried to understand the add.ctp from blog tutorial,
so i tried to implement it by myself
/app/views/posts/edit.ctp
    
<h1>Edit Post</h1>
<?php
    
echo $form->create('Post', array('action' => 'edit'));
    echo 
$form->input('title');
    echo 
$form->input('body', array('rows' => '3'));
        echo 
$form->input('id', array('type'=>'hidden')); 
    echo 
$form->end('Save Post');
?>
How can i get the 2 Inputfields side by side not about each other. Is it a thing from the css?

Many greetings

Marcus Radisch
Posted Dec 19, 2008 by Marcus Radisch
 

Comment

13 lesson 2 of posting questions

Hello

i've an little Question
I tried to understand the add.ctp from blog tutorial,
so i tried to implement it by myself
/app/views/posts/edit.ctp
    
<h1>Edit Post</h1>
<?php
    
echo $form->create('Post', array('action' => 'edit'));
    echo 
$form->input('title');
    echo 
$form->input('body', array('rows' => '3'));
        echo 
$form->input('id', array('type'=>'hidden')); 
    echo 
$form->end('Save Post');
?>
How can i get the 2 Inputfields side by side not about each other. Is it a thing from the css?

Many greetings

Marcus Radisch

Ok, now the problem is that you're spamming this tutorial: your question doesn't have anything to do with the subject of the tutorial. Go to http://groups.google.com/group/cake-php and ask the questions there.
But just because I'm nice: $form->input() is very thorough: it creates a div, a label and the input field. Try using just $form->text() and $form->textarea().
Posted Dec 19, 2008 by Adriano Varoli Piazza
 

Comment

14 Thanks

Thanks you for your help to the inputfields. I forgot that it is no forum but a tutorial. im sorry.
Posted Dec 20, 2008 by Marcus Radisch
 

Question

15 Regarding Installation of cakephp

I m new to cakePhp and i don't know how to install cake php please tell me the simple to install cake php i have already installed apache server and mysql and i don't know how to install cakephp in windows can anybody tell me step by step procedure of installition because i can't understand the manual
Posted Dec 20, 2008 by mona
 

Comment

16 no

I m new to cakePhp and i don't know how to install cake php please tell me the simple to install cake php i have already installed apache server and mysql and i don't know how to install cakephp in windows can anybody tell me step by step procedure of installition because i can't understand the manual
You're spamming. This is a tutorial about a search feature to cake, not a general help site. Try not to be so selfish (reading three comments above this, you would have seen this same answer). Go to http://groups.google.com/group/cake-php and ask there, possibly with something better than 'I don't know how to install cakephp'.
Posted Dec 20, 2008 by Adriano Varoli Piazza
 

Comment

17 Simply doesn't work

While it is true that not all programmers can write good tutorials and documentation, IMHO it's articles like this one that gives CakePHP the reputation of having poor documentation. It could and should have been a very useful article but just ended up being frustrating and depressing. At the very least one should have been able to follow it and end up with the advertised conclusion.

I can also write a tutorial that doesn't do what it's suppose to do (i.e. teach) but, like this article, it would not have been useful.

Here ends my bitter frustrated rant.
Posted Jan 1, 2009 by George Davison
 

Question

18 Search return nothing

Good tutorial

I read and practise all this tutorial's steps carrefully and double check it before being sure that search return nothing. I appreciate this, It looks useful way to set search machine on our web specially for cakephp programmer, but in fact the search return nothing.

Is there anybody having success with this? please let me know if there's something wrong or should be fixed in this tutorial.

really thank for you all who make this tutorial more clear and usefull
Posted Jan 5, 2009 by hermawan
 

Comment

19 Thank you

Good idea for a tutorial, having just started with the blog example i found this tutorial to be a welcome addition to my experimentating. Having said that i would like to say that 1 part is unclear, youre not very specific on where to place the search input. Im guessing its in the index.ctp and im probably wrong because when i submit i get the following error:

Error: The requested address '/posts/search' was not found on this server.

edit: I found that setting debug level to 2 removes the error and instead i get the same null string error previously mentioned. Although the posted solution is poorly documented for newbies as well.

edit again: For those running into the same issue: I found the _index() function the solution is referring to as part of the downloaded searchable.php file stored under models/behaviours on line 70.
Posted Jan 16, 2009 by wittedruif
 

Question

20 How do i fetch data from associated tables

Hi,
How do i fetch data from associated tables. One more thing.. when i edit a record does the search index table get updated automatically ...

Regards
Abhishek Jain
Posted Jan 30, 2009 by Abhishek Jain
 

Question

21 Searching in associated tables

Hi,
The example above doesn't search the associated tables.

e.g .. suppose i have 2 tables as
1.listings - id,user_id (e.g 1,1)
2. users - id,name (e.g 1,abhishek)

Now if am looking for all listings by abhishek .. how do i do that search


Thanks in advance

Regards
Abhishek
Posted Jan 30, 2009 by Abhishek Jain
 

Comment

22 errors

i got these errors.. what went wrong?


Query: search
Warning: SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'search' at line 1 in /home/jb/Desktop/cakeblog/cake/libs/model/datasources/dbo_source.php on line 439


Fatal error: Call to undefined method FormHelper::create() in /home/jb/Desktop/cakeblog/app/views/posts/search.ctp on line 4


Posted Mar 16, 2009 by zafi
 

Bug

23 feature doesn't work

result array from this tutorial is empty.
Posted Mar 18, 2009 by vislo
 

Question

24 Does it work?

Has anybody got this working? when I click 'Search' it responds with an empty table. Any help would help me out a lot.
Posted Mar 26, 2009 by Gearoid
 

Question

25 Does it work?

Has anybody got this working? when I click 'Search' it responds with an empty table. Any help would help me out a lot.
Posted Mar 26, 2009 by Gearoid
 

Comment

26 Current Data Requires Indexing.


This tutorial does not discuss indexing Current data. To do that please visit:

http://code.google.com/p/searchable-behaviour-for-cakephp/issues/detail?id=1
The second post has a nice example of how to index your current data. Just change the models you want indexed. Execute the page. Voila.

Posted May 19, 2009 by Ryan Barrett