nabeel shahzad

Sphinx and CakePHP

with 8 comments

For a project, I’ve decided to use the Sphinx search engine, and was looking for behaviors for CakePHP, to just make it much easier to implement. Since I’m using Cake 2.0, I could only find something that was for < Cake 1.3. So I decided to update it for use with Cake2.0, and it’s working beautifully with pagination.

It’s located in my github site:

https://github.com/nshahzad/Sphinx-CakePHP

The usage is exactly the same as the original (the link to it is above). The only thing is that it’s assuming you have the sphinxapi.php (which comes with the Sphinx source) extracted into Vendor/sphinxapi/sphinxapi.php (that’s where App::import() will look for it).

Written by Nabeel

January 3rd, 2012 at 10:01 am

Posted in CakePHP,phpVMS

  • mohamed ali

    نبيل الشحات يبدو من اسمك انك مصري وعندك خبرة ممتازة بالفريم ورك كيك بي اتش بي
    لماذا لا تعمل مدونة بالعربي لكي نستفيد مما علمك الله :)

  • Anonymous

    Google Translate leads me to believe this is Arabic – which I don’t know :)

  • fiveninesixtwosix

    Hello Nabeel, thanks for this code. I got a very simple question for you. If I have these on my controller:

    $sphinx = array(‘matchMode’ => SPH_MATCH_ALL, ‘sortMode’ => array(SPH_SORT_EXTENDED => ‘@relevance DESC’));
    $results = $this->Film->find(‘all’, array(‘search’ => ‘search string here’, ‘sphinx’ => $sphinx));

    How would I pass/display the $result on my View (index.ctp)? Can you give me a sample code please? I would really really appreciate it. Thanks.

    Lyman

  • Anonymous

    Hi Lyman, you would do it as you normally do with a model find, just set it to the view and iterate through it. It returns the same results as a normal find call, just filtered to the search.

  • lyman castro

    i think i know what you meant. One more question if you don’t mind please. I followed the steps on this website in installing Sphinx: http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows and made it work. I can search through command prompt, however, I get this error when I try to search from my CakePHP application: Notice (1024): Search query failed: connection to localhost:3312 failed (errno=10061, msg=No connection could be made because the target machine actively refused it.) [APPmodelsbehaviorssphinx.php. Do you have any idea why? Thanks.

    Lyman

  • Anonymous

    In that link, it looks like they configure it on port 9312 instead of 3312 (not sure why they changed it). I would change that port in the config, and restart searchd and try again.

  • lyman castro

    You’re a life saver Nabeel. I’ve been trying to figure this out for the past couple of weeks. Thank you so much. Take care my friend.

  • Anonymous

    No problem, glad it worked out!