Feed items

CakePHP + Sphinx: Applying filters to your search

Using the SphinxBehavior, you can filter out the search query based on the record owner. Imagine you have a ‘Clients’ table that belongs to an ‘Account’. In cakePHP, this requires you to have a field ‘account_id’ in the ‘clients’ table. In order to perform a query to Sphinx to return the records only owned by the current user, you need to do this:
$this->userinfo = $this->Auth->user(); //assuming you are using Auth component
$sphinx = array( 'index' => array('clients'), 'filter' => array( array( 'account_id', $this->userinfo['Account']['id'])), 'matchMode' => 'SPH_MATCH_ALL', 'sortMode' => array( 'SPH_SORT_EXTENDED' => '@weight DESC, @id DESC' ));

Google’s AJAX Libraries API: Loading your javascript framework faster

Recently, I discovered the power of using Google’s AJAX libraries API. This is a content delivery network that delivers popular open source javascript frameworks using Google’s “stable, reliable, high speed,  and globally available access“. Inside my CakePHP application, i simple added this on the header:

echo $javascript->link('http://www.google.com/jsapi');
echo $javascript->codeBlock("google.load('mootools', '1.2.1');");

It sure loads the mootools ‘core’ faster. Downside is that I need to manually include the ‘more’ library from mootools. I sure hope my hosting loads as fast as Google’s servers. LMAO

CakePHP with Sphinx: Searching from multiple indexes

I was using SphinxBehavior and I ran into a situation where i need to search from a specific index only. My sphinx installation currently has 3 indexes — clients, loans, and companies. By default, the SphinxBehavior searches across these 3. In order to search from 1 index only, you need to do this:

$sphinx = array('index'=>array('clients'),'matchMode' => 'SPH_MATCH_ANY', 'sortMode' => array('SPH_SORT_EXTENDED' => '@weight DESC, @id DESC'));

This makes the bahavior search for the record inside the index ‘clients’ only.
Hope this is helpful.

CakePHP: Dynamically adding a javascript in the header

Since cake 1.2, there is already an implementation to dynamically add (or include) a javascript file in the header. This is done in case you need to include a new js file in the header on one of your modules. This will prevent putting the code in the default layout and loading unnecessary javascript files in the header.
<?php echo $javascript->link('nameofjstoadd',false); ?>
This is called in the default.ctp using ‘$scripts_for_layout’ directive inside the header.