Debugging SQL commands in CakePHP

Feb 25 2011

I just don't get it - why is debugging with a real debugger such a black science in PHP?  You'd never see someone dump code to the screen in a C++ dev environment, yet based on what I've just seen while searching Google for some creative CakePHP techniques you'd never know that some PHP "developers" (no html tag that I'm aware of for 'air quotes'...) have an inkling that debuggers exist.

  I've used a variety of different frameworks over the past few years, and invariably there is some caveman-like debug() function available within the framework.  People then tout the awesomeness of dumping arrays of variables within the view layer - see this blog as an example :

Well what happens if you can't get the logic to run to the view layer?  What then?

There are tons more links like this on the interwebs.  I get that the intent is to "quickly" dump data to the screen - but really?  Are we in some sort of time machine and this is 1993 and no development tools exist?  Maybe it has something to do with PHP being able to be coded from notepad or vi.  Or the fact that anybody can quickly begin to code PHP while using less than stellar methodologies - every consultant has encountered a project where you come in after someone without much experience has totally flailed on a project that you were brought in to clean up.

Instead of going stoneage in trying to figure out the state of your data as it moves through your app by dumping info to the screen with print_r() or built in debug() methods, try uing an editor or IDE that supports a debugger.

Specific to CakePHP, to see the sql command being generated you can set a breakpoint within cake/lib/models/datasources/dbo_model.php on line 647, and then set your debugger to run to the breakpoint.  Viola, select this from that where.  Copy/paste the SQL syntax into your db gui and refine away.

Not sure why this bugs me so much, but it does.  One mans mission to bring joy and productivity to developers everywhere...