September 3rd, 2010
Sometimes when you try to add a foreign key in MySQL, you get this error:
ERROR 1005 (HY000): Can't create table 'my_table.#sql-a74_233' (errno: 150)
Every time I’ve come across this problem it’s been because my int types didn’t match. For example, I had an int(11) trying to reference a bigint(20) on a different table. If you change the int types to match, the problem goes away. Keep in mind that you may have to delete all the rows in your tables and/or remove existing foreign keys in order to be able to make the change.
Posted in MySQL | No Comments »
September 3rd, 2010
If you’re going to make multiple related updates to your database, you should use a transaction. Unfortunately Doctrine’s documentation on transactions, like most of the Doctrine documentation I’ve seen, kind of sucks. Maybe their whole “Unit of Work” class without error handling makes sense somehow but I don’t get it. Here’s what they have:
<?php
$conn = Doctrine_Manager::connection();
try {
$conn->beginTransaction();
$this->performCreatesOrUpdates($conn);
$this->performDeletes($conn);
$conn->commit();
} catch(Doctrine_Exception $e) {
$conn->rollback();
}
This is the right idea but it has the unforgivable flaw that it fails silently. If your transaction rolls back instead of commits, you won’t necessarily know about it. That’s bad. Here’s what I would have put:
<?php
$conn = Doctrine_Manager::connection();
try
{
$conn->beginTransaction();
$myObject->save();
$myOtherObject->save();
$conn->commit();
}
catch(Exception $e)
{
throw new Exception($e->getMessage());
$conn->rollback();
}
The code is mostly the same but that one crucial change—throwing an exception—makes a world of difference.
Posted in Databases, Doctrine ORM, symfony | No Comments »
September 3rd, 2010
This validator works for symfony 1.4 and is not necessarily backward compatible.
If you’d like to both validate and sanitize a URL in symfony, it’s pretty easy. First, put the following code in lib/myValidatorUrl.class.php:
<?php
/**
* myValidatorUrl validates and sanitizes a URL.
*
* @author Jason Swett (http://jasonswett.net/how-to-validate-and-sanitize-a-url-in-symfony)
*/
class myValidatorUrl extends sfValidatorUrl
{
protected function doClean($value)
{
$clean = (string) $value;
// If the URL doesn't start with "http", add "http://".
if (substr($clean, 0, 4) != 'http')
{
$clean = 'http://'.$clean;
}
// Add a trailing slash if the URL doesn't have one.
if (substr($clean, -1, 1) != '/')
{
$clean .= '/';
}
// If the URL still isn't valid after that, it probably wasn't close enough to begin with.
if (!preg_match($this->generateRegex(), $clean))
{
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
return $clean;
}
}
Then, in the form where you have your URL field, add the following line to your configure() method:
$this->validatorSchema['url'] = new myValidatorPhone(array('required' => false));
That’s all! Now, if someone enters a URL like “example.com”, it will get saved as “http://example.com/”.
Posted in symfony | No Comments »
June 21st, 2010
Posted in Photography | 2 Comments »
June 14th, 2010
Posted in Photography | No Comments »
June 1st, 2010
Has your son been hounding you for an electric guitar, but now that you’ve finally bought him one, you can’t stand the sounds you’re hearing from the garage? Or maybe you’ve picked up an axe yourself and realized you don’t know how to shred. Or perhaps you can pluck the old six-string cowboy okay but every time you try to play your favorite tune, it sounds a little off. I can help you fix these problems!
Why should you choose me for guitar lessons?
- Instead of teaching you Mary Had a Little Lamb or other tired songs no one actually wants to play, I’ll teach you whatever songs you want to learn. The Beatles? Rage Against The Machine? Lady Gaga? In the words of Roy Orbison, “You got it.”
- Whether you want to wail on the electric Van Halen-style or you’re more into Simon & Garfunkel, I can show you how to fine-tune your style.
- You’ll be learning from someone with over 10 years of playing experience.
- Your age and skill level don’t matter. I’ve taught both kids and adults, both novices and absolute beginners.
- It won’t cost you an arm and a leg. $10 per half hour and the first half-hour lesson is FREE!
So if you or someone you know is in desperate need of guitar lessons, why not get a hold of me and schedule your first free guitar lesson? Paired with a new guitar, you couldn’t ask for a better Father’s Day gift! Call or e-mail today:
Phone: (616) 856-8075
E-mail: jason.swett@gmail.com

Posted in Music | No Comments »
May 31st, 2010
This validator works for symfony 1.4 and is not necessarily backward compatible.
If you’d like to both validate and sanitize a phone number in symfony, it’s pretty easy. First, put the following code in lib/sfValidatorPhone.class.php:
<?php
/**
* sfValidatorPhone validates a phone number.
*
* @author Jason Swett (http://jasonswett.net/how-to-validate-a-phone-number-in-symfony)
*/
class sfValidatorPhone extends sfValidatorBase
{
protected function doClean($value)
{
$clean = (string) $value;
$phone_number_pattern = '/^(^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$)*$/';
// If the value isn't a phone number, throw an error.
if (!preg_match($phone_number_pattern, $clean))
{
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
// Take out anything that's not a number.
$clean = preg_replace('/[^0-9]/', '', $clean);
// Split the phone number into its three parts.
$first_part = substr($clean, 0, 3);
$second_part = substr($clean, 3, 3);
$third_part = substr($clean, 6, 4);
// Format the phone number.
$clean = '('.$first_part.') '.$second_part.'-'.$third_part;
return $clean;
}
}
Then, in the form where you have your phone number field, add the following line to your configure() method:
$this->validatorSchema['phone'] = new sfValidatorPhone(array('required' => false));
That’s all! Now, if someone enters a number like 123.456.7890, it will get saved as (123) 456-7890.
Posted in symfony | No Comments »
May 31st, 2010
I’ve improved the search and added some new sellers on Food Near You. Let me cover the new sellers first:
West Michigan Cooperative
West Michigan Cooperative is an “online farmers market” in Grand Rapids, Michigan. Their sellers include Crane Dance Farm where you can get beef, chicken, pork, duck, turkey and more; Mud Lake Farm where you can get lettuce and all that stuff; and three newcomers to FNY: H&W Farms, Jennings Bros. Stone Ground Grains and Joint Wells Orchards.
H&W Farms
H&W Farms sells mostly cherries. In addition to regular old cherries, they sell cherry juice and dried cherries. If you go there, I’m sure you will have a cherry good time!
Joint Wells Orchards
Joint Wells Orchards has apples, apricots, cherries, nectarines, peaches, pears, plums and a bunch of other stuff!
Jennings Bros. Stone Ground Grains
Despite the flour-ey descriptions of farm life you might hear, these guys are putting their noses to the grindstone to bring you several different types of flour and corn meal. If you’d like to go against the grain and buy your flour direct from the farm instead of at the supermarket, check out Jennings Bros. Stone Ground Grains.
Improved Search
A few days ago I announced that I had improved the FNY search. One of the half-measures of the improved search was that if you wanted to search for “Bob’s Farm,” you could do that, but you couldn’t search for grocers, restaurants or farmers markets by name. Just farms. Now you can search for anything by name. What’s more, if FNY can figure out exactly which place you’re talking about, it just cut to the chase and take you straight there. For example, searching for “jennings” puts you right on the Jennings Bros. page and searching for “fulton” takes you straight to Fulton Street Farmers Market. It’s a miracle of technology!
I’ll be adding more stuff soon, so keep your eyes peeled.
Posted in Business, Eating Local, Food Near You | No Comments »
May 29th, 2010
Last night I had a dream that I was in a house with a fly problem. Flies everywhere. My solution to this problem was to buy about half a dozen frogs and set them on the table. After I did this, I crouched over the table and stared intently at the frogs. “I can’t wait to see these guys go to work!” I thought. I watched them snap up the flies with their crazy little tongues. Eventually the frogs all turned into people, but they kept slapping up flies with their tongues, but now in super-slow-motion. Whoa!
The second part of the dream was even weirder and dumber. I had these frozen pizzas and each pizza had a little tab on the crust. The idea was that you had to shove it down just right in order to “start” the pizza, kind of like starting a dirt bike. If you shoved the tab down too soft or too hard, the pizza wouldn’t start. It really took some finesse. The instructions for starting the pizza were “1. Hold the pizza perfectly level and 2. Make sure the pizza knows that you’re bringing it into a loving world.” The idea with that second one is that you have to telepathically communicate your emotions to the pizza. That part was really tough. Eventually my friend Pat showed up with a pizza and we were both trying really hard to start our frozen pizzas. Eventually mine got too warm, the tab got all floppy and I said, “Screw it! I can’t start this pizza with this floppy tab!”
Posted in Uncategorized | 2 Comments »
May 28th, 2010
The following is for symfony 1.4 and is not necessarily backward compatible.
If you just want to completely change your title in symfony and you’re okay with wiping out whatever’s already there, it’s simple. Just use this in your action:
$this->getResponse()->setTitle('My New Title');
But what if you want to keep the first part of your app’s title and only change the rest? That’s also pretty easy. In apps/frontend/templates/layout.php, just change this
to this:
<title>The Static Part of Your Title <?php echo $sf_response->getTitle() ?></title>
Now when you do this in your action
$this->getResponse()->setTitle('My New Title');
The “The Static Part of Your Title” will still be there and only the rest of it will have changed.
Posted in symfony | No Comments »