Archive: October, 2010

Catch database errors before it’s too late

A quiet, little method inside model.php, can actually help us out quite a bit.
I am talking about the onError() callback.

Here’s a snippet from the API:

/**
* Called when a DataSource-level error occurs.
*
* @access public
* @link http://book.cakephp.org/view/1048/Callback-Methods#onError-1056
*/

function onError() {
}

And the little snippet below will log your DB error (presuming you are using ‘default’ connection) as well as the data that can be potentially causing the problem… well that would really depend on the actual error.

Thanks to NetersLandreau for providing the code:

class AppModel extends Model {
       public function onError() {
               $db = ConnectionManager::getDataSource('default');
               $err = $db->lastError();
               $this->log($err);
               $this->log($this->data);
       }
}

Simple, yet very useful. Nice.

FU … IE7

Sorry, but this one had me going nuts (not bolts) for 2 days.

When AJAX request was sent to the server in IE7, it would “magically” reset the session ID.
Of course, this would cause serious problems for the end-user.

Mind you, this worked perfectly well in every other browser.
I have searched high and low, lost sleep… pulled out a decent chunk of my hair, until I finally and accidentally stumbled onto a solution.

When dealing with older versions of IE and AJAX be sure to adjust your core.php:

/**
 * When set to false, HTTP_USER_AGENT will not be checked
 * in the session
 */

  Configure::write('Session.checkAgent', false);

The actual change is from default true to false.

p.s. If someone has any idea of the actual effect of this change, I’d be glad to know. So far, other than the fact that IE7 works as expected now, I see no difference.