Archive for the ‘Casey's Corner’ Category

14 Nov 08 _ MySQL Transactions With CakePHP 1.2

Whew, been a while since I’ve posted something useful.

Hopefully, everyone is aware of MySQL transactions and how useful they can be.  They come in especially handy when you want to save multiple related entries into different tables in your app.  Cake actually already uses this in a behind the scenes way with the Model::saveAll() function, which does some of this for you (you can read more about saving your data with saveAll() here).

But you can easily use Transactions for anything you want to do.

Note: for this to work at all you have to make sure whatever table you’re using transactions with are of the type InnoDB, and not the standard MyISAM type.

ALTER TABLE table_name type=InnoDB;

Ok, in your model, set the variable:

var $transactional = true;

Then, in a function you want to use transactions, call the datasource object:

$db =& ConnectionManager::getDataSource($this->useDbConfig);

Then, before you start interacting with the database, you call:

$db->begin($this);

If your action is successful, run:

$db->commit($this);

Or, if there’s a failure, you can roll back your DB with:

$db->rollback($this);

Note that your DB will automatically rollback if there’s not a commit() command anyway.

In the course of a Model class, the usage of these functions could look something like this:


class YourModel extends AppModel {

var $transactional = true;

function someCoolModelAction()
{

  # Grab DataSource Object #
  $db =& ConnectionManager::getDataSource($this->useDbConfig);

  # Start Transaction #
  $db->begin($this);

  /* SQL or Model Functions for whatever you need to do go here */

  # If your database interaction was successful, then commit the changes to the Database ( I'm assuming there is some boolean
  # variable, $successVar, that describes whether the action was successful or not).

  if ($successVar === true) {
       return $db->commit($this);
  } else {
       # If it wasn't successful, rollback any changes and you're good #
       $db->rollback($this);
       return false;
  }
}

}

As you can see, it’s pretty easy. And cool.

21 Jul 08 _ The Piggery Is Awesome

I’m one of the minority of meat-eaters here at AWP, and even then, I don’t eat a lot of meat (thanks a lot, Omnivore’s Dilemma), and we’re always on the lookout for places that serve up a good dose of ethically treated, naturally grazed animal slices. It’s kind of a treat when Mel and I do get some.

For the past few weeks at the Ithaca Farmer’s Market, we’ve noticed a new stand with a delicious-sounding name: The Piggery.  I like Pig (meat) and I like locally-raised food, so I knew we had to check it out.

Anyway, we made our first purchase there on Sunday: two pounds of tenderloin and a couple of bratwurst.  Last night, we rubbed the tenderloin in salt and fresh cracked pepper and tossed it on the grill.  Man, was it amazing.  The Piggery raises some good pig.  I’m sure it helps that they treat them well, and that they eat pumpkins. The meat was tender and naturally juicy, without any hit of that spongy texture that most store-bought tenderloin has.  The flavor was stronger and fuller than store-bought stuff as well.

So if you dig on pig and live near Ithaca, you should check it out.  I’m hooked.

17 Jul 08 _ Gimme, We’ve Missed You

Casey, our AWP spokesmodel

We love coffee.  It drives us.  It keeps us going through those long, grey days and those too-bright early mornings.  We also love Gimme! Coffee, our local roaster and snooty-delicious provider of the bean.

One of the many perks that comes with working here at AWP is free coffee.  And not just any coffee, we get free Gimme.

That is until we decided to experiment and try out another, Ithaca-based coffee roaster.  You know, for fun.

Well, we paid for that.  Our alternate, unnamed roaster’s coffee tasted like sweet dirt.  Or acid.  We couldn’t decide.  Maybe if a hobo decided to eat sun-baked dirt all day, and then we made him throw up after drinking too many shots of Green Apple Puckers, the resulting hot stew would be akin to what we had just purchased 10 pounds of.

Dammit.

Well, after courageously drinking as much of that swill as possible (with lots and lots of cream), we finally hit the new month, and could go back to our regular provider of coffee: our sweet, sweet Gimme.  Forgive us baby, please.  We didn’t know.

It all serves to illustrate an important point: never try anything new.

15 Jul 08 _ CakePHP 1.2 RC2 + Security Component

I’ve been wrangling with the Cake’s Security Component for the past day, not having the best time with it. If you’ve been trying to use it, you may have noticed that there seems to be near-total lack of documentation on it. Not cool.

So I was trying to use it to make some simple HTTP Authentication requests for a WebTree site of ours. It turns out that whenever the Security Component is initialized in a controller, it requires that all POST-ed data in that controller be validated through the Security Component. For this to work correctly, and not spit you out into a blank page “black hole,” you have to use the Form Helper for every form that needs to be submitted, making sure to use $form->end().

It looks like the Form Helper builds a Hash number based on the name of the fields included in the form and the Security Salt that you set in the config/core.php.

When the form is submitted, a function, __validatePost(), runs automatically, there is currently no way to turn it off, and tries to make the same hash value as all the fields present in the Controller’s $data variable. If the two hash values are the same, we know that no extra data is being submitted directly to the controller, and we proceed with the normal course of things. If they don’t match, we trigger the Black Hole callback function, and go down that route.

Except that in the current RC2 release (7296), there is a bug in the Security Component that will ensure that these two hashes will never be equal.

The problem is in line 662 of /cake/libs/controller/components/security.php:

$check = md5(urlencode(Security::hash(serialize($field) . Configure::read('Security.salt'))));

Should be:

$check = urlencode(Security::hash(serialize($field) . Configure::read('Security.salt')));

Take out the md5() function. Form Helper’s security function that generates the form’s hash value does not include the extra md5 hashing function. It looks like this was fixed in the nightly build.

This will alleviate some of your potential issues using the Security Component. However, if you just want to use some aspects of the component, such as HTTP Auth, there is no way to disable the POST validation, much to my chagrin. Maybe it will change in the future.

14 Jul 08 _ Sluggable Behavior + RC2

Ahh, yes. The shared pain/annoyance of upgrading Cake from RC1 to RC2 and finding that the SQL operator syntax has changed.  Sure it’s more sequre, sure it’s not as elegant looking, and sure there seems to be zero backward compatibility, but yeah, it happens.  It’s still a pre-release.  We deal.

So for those that currently use iano Iglesias’ super-helpful Sluggable Behavior, make sure to change line 121 from:

$conditions = array($Model->alias . '.' . $this->__settings[$Model->alias]['slug'] =>' LIKE ' . $slug . '%');

to:


$conditions = array($Model->alias . '.' . $this->__settings[$Model->alias]['slug'] . ' LIKE '=> $slug . '%');

The current version, 1.1.36, does not include this fix for Cake RC2. Without this, you’ll start getting duplicate slug values, lacking any numerical appending action.

Download Full Movie Online Abilify Wyvern download movie Little black book download movie In a dark place download movie Munich download movie Broken bridges download movie Shiloh 2: shiloh season download movie Scooby doo meets batman download movie ringtones for go phones uploading free ringtones make your own ringtones samsung awesome ringtones where to get free ringtones for att Meet the Spartans download movie Enter the Dragon download movie Congo download movie Airheads download movie Canvas download movie Cashback download movie Heavenly Creatures download movie Finishing the Game: The Search for a New Bruce Lee download movie The Level download movie Straight-Jacket download movie Deathline download movie Kung Pow: Enter the Fist download movie Showtime download movie Lean on Me download movie The Story of Anyburg U.S.A. download movie Tinker Bell download movie Under Siege download movie Killing Ariel download movie Street Trash download movie Smart People download movie Crimson Tide download movie Donald's Tire Trouble download movie The Donor Conspiracy download movie The Bank Job download movie The Final Conflict download movie The Lookout download movie Footlight Parade download movie Outside Providence download movie New York Stories download movie In the Heat of the Night download movie Taxi Driver download movie Carry on at Your Convenience download movie My Boss's Daughter download movie The Black Gestapo download movie 8MM 2 download movie Texas Rangers download movie Meet the Spartans download movie Enter the Dragon download movie Congo download movie Airheads download movie Canvas download movie Cashback download movie Heavenly Creatures download movie Finishing the Game: The Search for a New Bruce Lee download movie The Level download movie