ZendCon 09

I just bought my ticket for ZendCon, 2 hours before the discount ended, pretty stoked about that. This will be my first PHP/Developers conference and I am thoroughly looking forward to it. I never really had an interest in these before but my passion for PHP has grown tremendously this past year, so its only fitting that I go. I will be going with some friends and colleagues, so it should be a blast and not just me by my lonesome.

I am looking forward to taking the Zend certification test and the tutorial panels at the convention should teach me all I need to know before hand. On top of that, I'm quite excited to see Nate Abele talk about CakePHP (pretty awesome seeing as how everything else is Zend). If any of the small group of people reading my blog are attending, be sure to say hello and we can have lunch.

See you all at ZendCon in October.

Blizzcon 09

So I have not posted an entry for the past week, but I have a good excuse! I was attending Blizzcon and was doing coverage on Diablo 3 and Starcraft 2. If you unaware of what Blizzcon is, its a giant convention held by Blizzard Entertainment that basically showcases their games. They have large art and gameplay discussion panels, live game booths and a ton more for the 25,000+ attendees to divulge into. I however don't get to go to all of it because I am labeled as "press" (because I run sc2armory.com and d3armory.com), so most of my time is spent in the press room writing and covering panels. However, I have written 3 small articles detailing what I did for the past 3 days, but beware I ain't that great of a writer!

If you are a fan of Blizzard, or Starcraft 2, or Diablo 3, give a big shout out. And I must say, yes, these games are pure awesome.

  • Pre-Blizzcon: The Fansite Summit
  • Blizzcon Day 1 Round-Up
  • Blizzcon Day 2 Round-Up

Oh, and I will be starting my new job on Wednesday. Looking forward to it.

Custom method for grabbing a row based on its ID

More times then none when working with a database, you need a general purpose method for grabbing fields from a row that matches an id. Cake has built in magic methods based on the table column that do just that, for example findById() or findBySlug(), but sometimes it grabs associated data that you do not want. Below is a basic method that you can place in your AppModel to grab a row based on its id, with optional parameters for restricting what fields to grab or what associations to contain.

/**
 * Grab a row and defined fields/containables
 *
 * @param int $id
 * @param array $fields
 * @param array $contain
 * @return array
 */
public function get($id, $fields = array(), $contain = false) {
	if (empty($fields)) {
		$fields = $this->alias .'.*';
	} else {
		foreach ($fields as $row => $field) {
			$fields[$row] = $this->alias .'.'. $field;
		}
	}
	return $this->find('first', array(
		'conditions' => array($this->alias .'.id' => $id),
		'fields' => $fields,
		'contain' => $contain
	));
}

With a little bit of editing, you can make it work for other fields other then id. You must also have containable listed in your behaviors for the 3rd argument to work. If you still aren't sure how to use this method, the following examples should help.

// Grab a basic row based on id
$user = $this->User->get($id);
// Grab a row and limit fields
$user = $this->User->get($id, array('id', 'username'));
// Grab a row, fields and associations
$user = $this->User->get($id, array('id', 'username'), array('Country', 'Profile'));

How to do plurals in l10n and i18n

So this was a tricky one for me to figure out as it is not stated in the cookbook (that I could find). It took me having to edit the core CakePHP files and posting a Trac issue until the Cake team explained to me how its done.

First off you may ask, why would we need plurals in our locale files when we are defining everything our self? Well its quite simple, it is used within the Time helper (relativeTime(), etc) and can be used in conjunction with __n(). As with some developers, including myself, we would assume that plurals would be written as followed within our default.po file.

msgid "minute"
msgstr "Minute"
msgid "minutes"
msgstr "Minutes"

I was always under the assumption that Cake did some inflection and logic magic on the backend to determine the plural form, but that's not the case. You need to define the plural form manually using msgid_plural. The correct way to write plurals would be like so.

msgid "minute"
msgid_plural "minutes"
msgstr[0] "Minute"
msgstr[1] "Minutes"

"Its a bit messier, cant understand why it wouldn't be msgstr_plural or something equivalent, but that is how you do it." (Has been answered below, its used for languages with multiple plural versions). For future reference, all CakePHP internal code that uses pluralization uses the strings in default.po, so you should place the locale strings there. If you place them in a separate locale file, you can then use __dn().