Tag: cakephp db

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.

Changes to database config keys in RC1

There are a few important changes in the database configuration, which I wanted to quickly point out:

1. ‘connect’ key is gone.
For example, if you had ‘connect’ => ‘mysql_connect’ you will need to replace it with ‘driver’=>’mysql’

2. New key: ‘persistent’=>false
This one is very important and you definitely want to add it to your configuration file.
It had caused me some pain, before I noticed this ticket:
https://trac.cakephp.org/ticket/4873

 

I want objects, not arrays!

A few times I’ve heard people complaining about the fact that CakePHP’s find methods return data as arrays and not as objects. “It’s not OO!”, “I want access to Model methods!”, etc. etc. if you’ve followed cake for a while, you’ve probably heard the same.

It’s a bit of a debate as to why you’d want to have objects vs arrays, but I can tell you that most arguments I’ve read call for some breaking of MVC and often arise due to not really understanding the way CakePHP does certain things. Personally, I have nothing against working with arrays especially if my ultimate goal is to just display some data in the view. You can do a quick search at the google group to get views from both sides of the story, so I won’t repeat them here, but please keep in mind that it’s always a good idea to figure out how to use a framework (any framework) to accomplish what’s needed, rather than how to make it work the way you prefer.

So can you return objects from find methods, instead of arrays? Actually it’s very simple, here’s a little gem of code that was posted at the google group, by gwoo:

[sourcecode language='php']
function afterFind($results, $primary = false) {
if($primary == true && !is_object($results)) {
return Set::map($results);
}
}
[/cc]

Just to clarify, this is supposed to be added to your app model, so all of the results returned by your find methods, will be automatically “objectified”.

Obviously you’d probably want to expand on that a little, but even with this simple addition you can easily get access to the much desired objects. Just remember to keep in mind that if you do use this approach, it should not be an excuse to break MVC and other conventions.

Don't forget your primary key

If are you are using a non-standard column name for your primary key (something other than ‘id’), maybe from a legacy database, it is very important not to forget to set the var $primaryKey in your model. This little oversight can break a lot of things without giving any clue as to what the problem is.

A common issue is to have column named ‘ID’ instead of ‘id’, which will cause CakePHP to behave strangely and it’s not very obvious as to what is really going on.

So, if you are having some strange issues with find methods or the data is not comming back as you’d expect, always double-check your primary key name.