Archive for the ‘This Site’ Category


Use Functions from Other Controllers While Maintaining MVC Architecture in CakePHP

Wednesday, December 10th, 2008

UPDATE (7/22/2009)

requestionAction may not be the best solution. Read this.

At my day job, I’m working on an application to keep track of specimens for our lab. A specimen is sent to the lab, then divided into aliquots which are put into boxes and stored in freezers. The previous sentence ought to give you some idea of the architecture of the database, which in turn drives the Model for my application.

To take a step back for a second, I’m developing the application using the CakePHP framework which uses MVC architecture.

As you may have guessed I have specimen, aliquot, boxes and freezers tables. In turn then, I have Specimen, Aliquot, Box and Freezer Models.

The trick here is that I want to alert users when there are aliquots in the system that have not yet been assigned to boxes. It’s a simple query:

SELECT COUNT(aliquot.id)
FROM aliquots
WHERE aliquots.box_id IS NULL

The problem is that I want the number of unstored aliquots to be displayed on every page in the left column as a persistent reminder that there are are aliquots that need to be put away. I want to do that in a way that maintains the MVC architecture and doesn’t violate the DRY philosophy.

Since the query is run on the aliquots table and each view is generally specific to it’s own model, I either have to run a recursive query to access data across models–which adds overhead–or implement the solution below which lightens the load a bit and is a more elegant bit of code. (Tip of the hat to Jon Bennet for offering the solution.)

The solution involves CakePHP’s requestAction().

I can define the method in my aliquots controller and call it from anywhere. So if aliquots_controller.php has a method that retrieves the data from the model (in this case called ‘unstored’) I can simply put the following code into my layout:

$unstored = $this->requestAction('aliquots/unstored');
if(!empty($unstored)) {
echo $html-&gt;link('<strong>' . $unstored['unstored'] . ' aliquots have not been stored.</strong>', '/aliquots/store');
}

I only have to define the method once to use it throughout my application. Problem solved.

Careful Where You Point That Thing

Friday, April 18th, 2008

In four recent instances I’ve written something or mentioned something in a podcast where someone directly related to the subject found the post/podcast. That seems to me to be a relatively recent phenomenon.

Instance one: In a recent Minneapoliscast podcast, we discussed the demise of No Depression’s print magazine. Soon after Kyle Matteson and Steve McPherson attended SXSW. Who should attend Reveille Magazine‘s day party but Peter Blackstock, co-editor of No Depression. Peter actually discussed listening to the podcast w/ Kyle. I had to review it to make sure I didn’t say anything asinine.

Instance two: I mentioned drop.io the other day here. Who should comment on the post but someone directly involved with the company.

Instance three: I made a few off-hand comments at MinneWebCon via Twitter. Who responded to me directly? One of the organizers of the event.

Instance four: I wrote about APML here earlier this week. Who added the post to his ma.gnolia links? Chris Saad, one of the founders of apml.org.

We’re no longer sequestered in our rooms in the cold glow of a CRT. We’re talking and exchanging ideas. How you doin?

Upgraded to WordPress 2.5

Wednesday, April 2nd, 2008

WordPress 1.x was the first CMS to get me to move away from my old home-brewed CMS I used to use for blogging. Yesterday I upgraded this blog to Worpress 2.5. Even though you can’t see it, the admin area has a whole new look and support for audio and video in posts. I’m going to upgrade Minneapoliscast next to see how well it works with a podcast. Then a handful of other sites including the site I maintain for work.

This is my first post with the new interface. It’s taking a little getting used to, but once I figure out where everything is I think I’ll like it. I can’t say I can give WordPress a fair review in comparison to other CMS’s since I’ve never used Movable Type, Joomla, Drupal or any other of a cornucopia of blogging applications and content management systems.

Anyone else have experience with these others? How do they compare?

Hello world!

Wednesday, March 26th, 2008

I’m leaving the title. I’ve actually been blogging since 2002. This year my old blog was lost in what I can only call the great server crash of aught-eight. So here I am with a shiny new URL named after me. Anthony G. Thomas. You can call me Tony.

Hello World!