Monthly Archives: August 2011

Cake DB Update Shell

Time for another useful CakePHP code example. This was created for our hosting system at work, and is used to manage DB updates on installs of the hosted websites. I have removed some code to simplify this example, mainly relating to our directory structure and to the ‘global’ database updates. Once you understand the code below, it should be relatively simple to extend it to work with other setups. We also have code to support ‘script’ updates that are held on a Task, but I’ve removed that as well.
Read more »

CakePHP, Custom Queries, and the Result Array

Note: Apparently this no longer works in Cake 2.x. I’m a little disappointed by that, but that’s the way it is.

Just a quick post about a wonderful trick I stumbled across last night while experimenting with Virtual Fields on models in CakePHP. Since I started using Cake, I’ve been annoyed that when I ran a custom query, the results would come back in the ‘[0]’ index of the result array, because Cake couldn’t tell what tablename they were from. I’d experimented with a couple different ways to alias them to try and get them to work,┬ábut none of them worked. I eventually gave up, and just dealt with it.

So, last night while setting up my first virtual field, I looked at the debug log. There, I discovered this query:

SELECT 
    `Package`.`id`, 
    `Package`.`name`, 
    `Package`.`price`, 
    (CONCAT(`Package`.`name`, " ($", `Package`.`price`, ")")) 
        AS `Package__list_name` 
FROM 
    `packages` AS `Package`

The key part to notice is the alias it gave to the CONCAT() field. ‘Package__list_name’. That allows Cake to properly identify the model that the field belongs to when retrieving the results, and it can then put it into the array properly, resulting in this array structure:

array (
  0 => 
  array (
    'Package' => 
    array (
      'id' => '1',
      'name' => 'Basic MU* Hosting',
      'price' => '5.00',
      'list_name' => 'Basic MU* Hosting ($5.00)',
    ),
  ),
  1 => 
  array (
    'Package' => 
    array (
      'id' => '2',
      'name' => 'Basic Web Hosting',
      'price' => '5.00',
      'list_name' => 'Basic Web Hosting ($5.00)',
    ),
  ),
)

I hope this tip is as useful to others as it will be to me. It may not seem like much, but it will be very nice to be able to place items where I want them in the result arrays.

Ham Radio Update

I’ve been getting much more active in ham radio lately, and have helped a local group I joined put up a website. I also just finished installing an antenna on my car to go with my handheld radio. Feel free to go read my post over on the site I set up for them.

I should also soon have an HF base station. I’m simply waiting for the check for some side work to come in. As soon as it arrives, I’ll be buying an IC-756 radio. It’s about 10 years old, but I couldn’t care less, and can’t wait to be able to listen to and talk to people all over the US and (hopefully) the world.