Programming, CakePHP and commentary from Britain.

Popular

CakePHP Livesearch
CakePHP Sessions
VBScript & Excel
VBScript & Oracle

Search

E-mail

kdobson@gmail.com

Subscribe

RSS 2.0

Viewing posts in the ‘Weblog’ Category

Really getting MySQL to work on Leopard

There are numerous posts that are floating around the blogosphere and forums on getting MySQL working on Leopard (as in “cannot connect through /tmp/mysql). Some of them seem to be a bit over a top, and the solution I found (which may exist elsewhere) is quick and easy.

Open /etc/my.cnf as root:

	$ sudo nano -w /etc/my.cnf

Replace all occurrences of

	socket = /var/mysql/mysql.sock

with

	socket = /tmp/mysql.sock

Restart MySQL using either the preference pane or with the command line, and you should be away.

I think your mileage may vary, but for me, CocoaMySQL nor Ruby could connect to the MySQL daemon out of the box.

Conditional debug statements in Visual Studio

It has taken a me a long time to finally get this neat Visual Studio trick working. Despite it being a very powerful addition to the development and debugging process, there doesn’t seem to be much information about it, apart from the small entry on MSDN discussing the preprocessor.

To perform different actions or execute different blocks of code depending on your build configuration (think debug or release), you will need to define a new symbol for your build configuration. In my scenario, I was interested in executing a block of code whilst in debug mode, but not perform it in release.

  1. In Visual Studio select the Project menu and click “(Project) Properties” at the bottom of the menu.
  2. Select the Build tab on the left. Make sure your Configuration is set correctly - it should be debug for this example.
  3. In the Conditional compilation symbols text box enter: /define DEBUG
  4. Save the configuration and close the properties window.
  5. In your code, where you want to run a commands depending on the current build:
    #if (DEBUG)    
      Console.WriteLine("You'll only see me in debug mode..."); 
    #endif
  6. Then give it a quick compile and witness the magic!

There are a couple of uses that spring to mind - debug trace messages to help monitor what your code is doing, only deleting a file in release mode (if you’re downloading a file from the Internet repeatedly), outputting detailed exceptions in debug, but nice error messages in release.

This is possibly a very basic trick that everyone has been using for years, but I’ve only recently been making the most of it.

If you’ve used this before - in what context do you employ it?

CakePHP: Courteous redirection after user login

There are several good user authentication/registration/login systems available for CakePHP (especially version 1.2), but I decided to roll my own. This offers a good learning exercise, in addition to the benefit of knowing where all your code is and what it’s doing.

The latest little addition I’ve made to the code allows you to redirect the user back to where they wanted to go before they logged in. For example, I want to add a recipe into my online multi-user cookbook web application. If I click an “Add Recipe” button and get taken to a login form, I don’t want to then be redirected to the front page once I’m logged in - I want to go straight back to adding a recipe.

The basic logic of the login system is:

  1. A function in /app/app_controller.php to check if the user is logged in. If they’re not logged in, redirect them to the login page.
  2. A login form to talk to a controller and model which do all the processing/checking to see if the details are correct.
  3. A line of code at the end of all this to redirect the user to the front page (or members area).

By adding a tiny snippet of code to the check-if-user-is-logged-in function, and revising the redirect after login, the user will get a much more pleasant experience:

function checkIfLoggedIn() {
if(!$this -> Session -> check('user')) {
	$this -> Session -> write('lastPageVisited', $this -> params['url']['url']);
	$this -> redirect('/users/login', null, true);
	}
}

Then when, in your controller, you decide that the user is all logged in (in this example you’d set the user variable in the session), use the following snippet:

$this -> redirect('/' . $this -> Session -> read('lastPageVisited'), null, true);

Pretty rough and ready (forgive the prepending slash), but it works for my needs. If you do this, you’ll probably want to check the lastPageVisited variable to ensure it exists.

Do you use CakePHP and go about this a different way? Would love to hear different methods…

Encoding coordinates for efficiency

The folks over at Soul Solutions published an article (Encoding for performance, Soul Solutions) outlining the steps for implementing an “Encoded Polyline Algorithm Format” (Encoded Polyline Algorithm Format, Google) - an algorithm for reducing the data overheads when sending long lists of coordinates over a network.

I have been aware of this for a while, but - unfortunately - have not had the opportunity to put it into practice. The duo over at Soul Solutions work primarily with Virtual Earth (Microsoft’s on-line mapping system), so tend to focus on loading speed and ensuring things are as snappy as possible. My scenario was a little different; generating KML files of hundreds of polygons based on geographical boundaries.

Read the rest of this entry »

CakePHP Livesearch

This is an updated version of “CakePHP Livesearch” - published here in 2006. That version was designed for CakePHP 1.1, whilst this is a more polished and modern solution for CakePHP 1.2. The two tutorials are not compatible with each other due to changes in the framework. It is recommended that you stick with this one, and use CakepHP 1.2.

A while ago I wrote a tutorial covering how to get livesearch functionality working in CakePHP. A lot has happened since then - new versions of the framework have been issued, my knowledge of all things Cake, PHP & Ajax has deepened. Here’s hoping that my ability to communicate has, also.

There was a lot of positive feedback from the last tutorial which has spurred on a newer, revised tutorial. The contents of this post relate to Cake 1.2 (still in beta, but very usable). Whilst much of the steps are the same, there are a few subtle differences, and I have tried to clean things up a little better. So, without further ado…

Read the rest of this entry »

Overheard

Overheard as I was walking down the road past a group of gas engineers working in a hole in the pavement:

Well it’s not this one that’s leaking…

Reassuring.

Employer Honesty

When I come to the end of my degree, in June, I shall most likely be pursuing a job. Hopefully I’ll have the luxury of choice so that I can do something that I want to do and with a company who I respect and admire the operation of.

Unfortunately, without getting to an interview, finding out what some job roles entail is becoming increasingly difficult. Some companies seem intent on shrouding their job descriptions in a haze of what can only be described as bullshit. Myself and Matt have been browsing various graduate employment directories to discover that a sizeable handful of companies don’t seem to grasp that their pitch is being directed at fresh graduates - we don’t understand what “supporting the successful operation of international business practices” means, nor do we care that every role you’re offering us has “manager” in the title - everyone knows a graduate is very very unlikely to be plunged into a managerial role, we simply don’t have the experience or knowledge.

I can’t get my head around why multi-nationals, who must be doing something right to reach such status, can’t sort out graduate recruitment techniques - how these companies are going to recruit the necessary individuals to keep their company running in several years is beyond me.

It would be nice if all companies could be honest and clear with their descriptions - we do them the service of concise and to the point CVs and applications; I do Software Engineering, not “A complex understanding of the precise involvements of multiple components and policies through their implementation and review into small-large scale computer system programs.”

ReviewMe Storms Blogging World

If you’ve not yet heard of ReviewMe, then you soon will. It’s a new service which plans to pay bloggers to write reviews on products and services that advertisers submit.

The service is two pronged - it allows advertisers to submit a product or service to be reviewed, optionally specifying which theme of blog they want to review their product (tech, politics, health etc.). The review then percolates down through to all the targeted bloggers, some accepting to do the review, some not. Read the rest of this entry »

A Weekend In London

I spent the most recent weekend galavanting around our good country’s capital - London. Driving up on the Friday afternoon, and public-transporting around the centre, gave insight into a few failings of the transport system established in London.

Read the rest of this entry »

How low can you go

Myself and the University chaps were out for a few drinks tonight - fairly harmless stuff, having a few pints, enjoying a bit of light banter; nothing untoward. All of a sudden a woman approaches us with a plastic-coin-collection thing, you know - where you slot the money in the top. I’d seen her come into the bar and observed how out of place she looked - far too old to be in the heaving student bar as she stood rather uncomfortably in the corner without a drink. We were first on her collection round - presumably after she’d picked up enough courage. She had a moderately convincing lanyard-attached charity ID badge - unfortunately the logo on the ID didn’t look a thing like her chosen charity (we’d had a flyer through the door from Great Ormond Street Hospital only a few days before). She proceeded to recieve two prompt “No” responses from us (presumably to be three, had Ian not re-invented the Universe en-route to the bar) and continued on around the bar - I saw at least half a dozen people slot their pound coins into her cursed collection pot.

Taking advantage of a charity which supports children with lukeamia to get a few extra pounds is one of the most sickening things I’ve ever witnessed. Absolutely horrendous - I can’t even begin to imagine how these people being to think about sleeping at night. I don’t really want to swear on here, in case my Mum ever finds it - but if I were to swear, this is the post in which it would be. Sickening.