programming

CakePHP database config quick-tip

Most CakePHP programmers have seen this simple way of storing the development and production database settings in one file:

  1. class DATABASE_CONFIG {
  2.     var $prod = array(
  3.        
  4.         'database' => 'live_mysite'
  5.     );
  6.  
  7.     var $test = array(
  8.        
  9.         'database' => 'local_mysite'
  10.     );
  11.  
  12.    // the construct function is called automatically, and chooses prod or dev.
  13.     function __construct() {
  14.         //check to see if server name is set (thanks Frank)
  15.         if(isset($_SERVER['SERVER_NAME'])){
  16.             switch($_SERVER['SERVER_NAME']){
  17.                 case '127.0.0.1':
  18.                     $this->default = $this->test;
  19.                     break;
  20.                 default:
  21.                     $this->default = $this->prod;
  22.                     break;
  23.             }
  24.         } else { // we are likely baking, use our local db
  25.             $this->default = $this->test;
  26.         }
  27.     }

If you haven’t, check out Edward A. Webb’s original post from 1998 2008.

This same technique can be used in the bootstrap.php file for other settings that change based on the server, for instance GoogleMap keys:

  1. if ( strpos( $_SERVER['HTTP_HOST'],'MySite.com') !== false)
  2.     Configure::write('GMAP_KEY', 'ABQIAA…qcPrtZgKHQ');
  3. else
  4.     Configure::write('GMAP_KEY', 'ABQIAB…e6qfWeeY8Q');

However, when baking a model, the database file only defines two types: prog and test so you pick one and end up with a model file with a line like:

  1. var $useDbConfig = 'test';

in your baked model that doesn’t cause any problems right up until upload it to your live server late one night and start getting errors about being unable to make a connection- guess who did that?

The solution is simple: just define a default

  1. var $default;

somewhere in the file, now when you bake cake will give you the option of using the default connection which won’t add any problem-causing $useDbConfig to your model.

Hope this saves someone 20 minutes :)

Adding robots meta-tag to a CakePHP view

Here’s a little how-to I discovered a few months ago while I was working on SimcoeDining.com and realized that Google was indexing a whole lot of mostly blank pages.

To solve this, I figured I’d better put a no-index on those pages fast; after mucking around with CakePHP’s html->meta handler (because I wanted the meta tag in the header where it belonged) and not finding anything, I came up with:

  1. $html->meta('robots', null, array('name' => 'robots', 'content' => 'noindex') ,false);

BTW: The pages where empty at the time because there wasn’t a lot of data in the system, so a lot of searches where coming up empty- that’s been “fixed” now too.

Hopefully this saves someone else 20 minutes :)

MySQL’s Order By Field – this time we do it the easy way!

A few years back my company was writing a search engine for a Toronto restaurant directory. The search used tag-words to search for venues. Every time a search was done, venues where assigned a score based on how relevant there where to the tags being searched for. Now after the score was calculated, I ended up with a list of venues ordered by their score.

Now the messy part…
In oder to display these in a list, the venues had to be pulled out and listed in that order; as well, if there was more than 40 venues, they had to be split and displayed on two or more pages. In the end, it turned out the quickest way to write it was to query MySQL for each venue one at a time. Needless-to-say not an optimum solution.

…and the right way- 4 years later
When this came up last summer during the re-write of SimcoeDining.com, I figured there there had to be a proper way of solving that problem, and I found one in Imthiaz’s blog entry:

  1. ORDER BY FIELD(Venues,13,22,42,1,11)

See with MySQL (at least since v5) lets you specify a field and values to sort them by which nicely fits into CakePHP’s Paginator too.

Halifax-Restaurants attracts Nova Scotians

Well this summer’s project, Halifax-Restaurants.com was a bit of a disappointment (stillborn would be a bit cruel), mostly due to my unfamiliarity with the CakePHP framework I used as the underlying framework, I was surprised to see that it was actually starting to attract visitors from the Halifax area, according to Google Analytics.

Halifax-Restaurants screenshotThat’s the good news. The bad is the time I have to work on it. Ideally, I’d like to re-write nearly everything about it, then fix up the layout, which is too ad-heavy, as opposed to content for me right now. I have a few ideas about how that can be improved, but the fact is, adding content is the biggest task right now.

Saved by the book

The last few weeks I’ve been selling off a bunch of older titles I had laying around (instead of working on this blog), one of them that almost went off to the local BMV was PHP 5 In Practise; Fortunately, I passed on that one and tonight, while trying to finish off a tag-generator for Halifax-Restaurants.com, it saved my tail at least once. When I originally reviewed it, I was impressed with the amount of material in it- it’s still one of the best books out there when you’re looking for something like- “how do I remove white-spaces from a string” and such basic but necessary things.