[Solar-talk] Making $record->save() return boolean?
Paul M Jones
pmjones at paul-m-jones.com
Tue Mar 11 08:56:12 CDT 2008
Hi guys,
Currently, when you save a record, you need to do so in a try/catch
block so you can get information about any errors that cropped up.
try {
$record->save();
} catch (Solar_Sql_Model_Record_Exception_Invalid $e) {
// process $e for errors
}
It's really verbose, but until recently I wasn't able to think of a
way to get around it *and* maintain accessibility to error conditions.
However, I realized that I can have the save() method do the try/catch
block for you, and return boolean true/false to indicate success or
failure. At the same time, the record can retain the the exception if
something went wrong. The usage would look more like this:
if (! $record->save()) {
$e = $record->getSaveException();
// process $e for errors
}
It also lends itself to cleaner if() condition combinations:
// adding a record
if ($this->_isProcess('save') && $record->save()) {
$this->_session->setFlash('add_ok', true);
$this->_redirect("record/edit/{$record->id}");
} else {
// not a save process, or record save failed
}
For record saving, which is a frequent occurrence, I think this is a
valuable convenience.
Anybody have strong feelings about this one way or the other?
-- pmj
More information about the Solar-talk
mailing list