Custom Error Handling in CakePHP when debug = 0

Not sure if this is the most correct way to handle this, but I wanted to use a custom error method in my controllers. However, if you’ve been working with CakePHP a bit, you’ll notice that Cake automatically throws a 404 error when you set debug to 0.

To get around this I opened up /cake/libs/error.php and commented out the following lines:

/*if (Configure::read() == 0) {
$method = 'error404';
if (isset($code) && $code == 500) {
$method = 'error500';
}
}*/

This seems to solve the problem, for now. Hopefully this will be fixed in a future version of Cake because I detest modifying core files.

Quick 'n' Easy Way to Dynamically Set Per Page Limits with CakePHP's pagination

Maybe this is old news to some Cake veterans, but it took me longer than I would’ve liked to figure this out.

I have a settings table which holds a “per_page” variable and value, to allow admins to set their own per page limits for their site. However, because Cake defines $paginate variable like so:
public $paginate = array(..);
you cannot define a Configure::read or $variable to pass the “limit” key along. And pasting $this->paginate in 4 or 5 actions was not my idea of a good time.

After some head-scratching it dawned on me – define the other keys (fields, order, etc) in the $paginate variable and then simply define the “limit” key in the action itself!

Now here’s what my Controllers look like:
public $paginate = array(
'ModelName' => array(
'order' => ..,
'fields' => ..,
"conditions" => ..
)
);

Then in my action, I simply do a quick check to see if the per page limit has been set (don’t want to limit 0,0!!), and if so, set that as the limit variable:
if( $limit = Configure::read("per_page") AND !empty($limit) ) {
$this->paginate["ModelName"]["limit"] = $limit;
}

I couldn’t believe how easy it was 😀

Easy way to use Zend Framework in CakePHP

More cursing this morning as I tried to integrate Zend Framework’s GData within my project. After a bit of trial and error I came across this simple easy solution:

1. Save the ZF folder as Zend in your [app]/vendor folder
2. Create a file named zend_include.php in your [app]/vendor folder and paste the following code in it:
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . dirname(__FILE__));
3. To use in Cake, do this:
App::import('Vendor', 'zend_include');
App::import('Vendor', 'Zend_Gdata_YouTube', true, false, 'Zend/Gdata/YouTube.php');

(Replacing Gdata_YouTube and /Gdata/YouTube.php with the proper Zend Library you’re trying to use)
4. You can then call
$zend = new Zend_Gdata_Youtube(); // etc

I wish I could take full credit for this but it was this comment that finally got me on my way.

Quick tip for the form helper in CakePHP when editing

While working on my latest crazy idea, I was testing the edit function of a particular section and realized something alarming:

If I had an error while editing, the form helper was changing my /edit/id form URL to /add!

I could not for the life of me figure out what the problem was. After an hour of various curses and attempts to correct the issue, I stumbled across this little bit of information:

You need to add a hidden id field with the id for the record as the value in order for the edit URL to persist. D’oh!

I simply added this to all my edit views and voila! It worked! Hopefully this will help someone else 🙂

The time does fly

I’ve been busy busy busy the past couple of months. I wrote an entire CMS with Kohana, and have been letting it simmer a bit to give my brain a chance to breathe. I’ve found that if I complete a project and then jump back into it right away, debugging, making notes for missing features, etc, that I get burned out and end up scrapping the entire thing.

Because at some point I plan to sell this CMS (very niche-oriented and AFAIK there are no other CMS’s specifically built for this niche), I decided to go with some sort of licensing. I’ve used phpAudit — really, the standard licensing program for a lot of people — in the past but to be honest I was never overjoyed with it. It always seemed too bulky and I had a lot of problems when I used it before (mostly, timeouts with people’s licenses and I never could get the local license thing working correctly). I also found a licensing module for WHMCS that looked really promising; however, I don’t have $100 for the module + $17/month for WHMCS (besides, I installed the trial of WHMCS and while it seems like an awesome software for hosts I found it way too much for what I needed.

So I decided I would start coding my own licensing app. And I would use cakePHP for it.

Well, I actually started out using Kohana again but I found myself goofing off more and more every day I worked on the app, which is a surefire sign that something isn’t right with it. I think of it as akin to writer’s block. You get to a point with a story (or in this case an app) and you hit a brick wall. It’s nothing you can put your finger on but you just can’t get any farther without feeling like you’re trudging uphill through molasses.

So I started trying out some things with cake and as I sit here, two weeks later, I’m now farther with the app than I was in 3 1/2 weeks with Kohana, and with code that looks a lot neater and less repetitive. I’m pretty pleased with it so far. Hopefully I will be able to start coding the frontend by next week.

On a totally different front I’ve managed to maintain my weight and actually lost a little more than 1/2 a pound since last month — not bad for someone who is not going out of their way to be Health Gal. I’m still doing my best to eat right and watch my portions, I’m just  not being so strict with everything. Trying to keep a habit of taking a walk with my kids after dinner, so that should help too.

Why is it so hard to make a choice?!

So I’ve been working on OMGSUPERAWESOME© idea for almost 2 months now. I wrote initially about how I was using Kohana, then started trying out CakePHP and CodeIgniter when I saw that the chances of Kohana’s 3.x codebase being compatible with 2.x were slim to null (heh – I love programming humor :))

I also have been dabbling a mite in Zend Framework, which, I have to say, I find really complicated but also kind of interesting (although the interesting may due to it being a new framework). I’m a bit wary of it, though, since a very simple query to retrieve 3 rows from the database usually gives me a benchmark of .2 – 1.2, whereis in CodeIgniter I’d get a .01 – .3 and Kohana, a .01 – .8.

I think I will probably end up returning to Kohana because the framework just feels right — I feel like it gives me what I need to help me code faster AND better, while allowing me to do the things I want to do. ZF feels like it would end up being way too much for this project and it just doesn’t feel right to me the way Kohana does. And you know, I would have probably stuck with Code Igniter if I didn’t feel like it was 2x as much work to get certain things accomplished (vague, I know; perhaps a topic for another post).

My biggest concern with Kohana is the compatibility between version issues. However, I did read that v3 will likely not be out until mid to late summer this year, so I suppose we’ll just have to wait and see for now.

Just a couple of updates

I am still working on my OMGSUPERAWESOME project, although I hit a stumbling block and have spent the last 3 or 4 days tearing my hair out.

I mentioned how I had started developing with Kohana. Everything was working well (for the most part)….then I read a post from one of the lead developers that Kohana 3.0’s code would most likely be incompatible with 2.3.x’s. Apparently this is fairly common with Kohana, and I suppose depending on your point of view it can be a good thing or a bad thing. For me this was very bad.

Maybe I’m just stubborn but I like the tools I’m working with to remain somewhat stable between versions. I don’t want to be told that the code I’m writing now will have to be completely reworked to work with the new codebase. That’s a humongous waste of my time, and any users that may be using my software.

So I was playing with the idea of going back to good ol’ CI, but I gotta say — I really did not relish having to go back to writing 2 to 3 times as much code just to get a semblance of the functionality offered from Kohana or one of the other frameworks. Maybe I’m lazy, or maybe I’m just looking to increase my productivity. Maybe both.

So I started playing with CakePHP again, and spent the better part of last Sunday measuring the difficulty of setting up the MVC for a fairly straightforward part of my app. It wasn’t as hard as I was afraid it would be so I continued working with it and slowly have become more impressed with it.

I’m still a little hesitant to say for sure that I will end up sticking with Cake; I’m concerned that if I release my app and someone has an issue that isn’t directly related to my code (ie, it’s an issue having more to do with Cake) it’s going to be a lot harder for me to track down since I’m not as familiar with Cake. I’ve “made up my mind” that I was just going to say **** it and go back to CI only to remember why I was so frustrated with CI in the first place. On the other hand, Cake is a lot more rigid and I find that I end up bending my app around it, whereas with CI it’s more of a silent partner. Of course, with CI it’s pretty easy to end up with a lot of spaghetti code.

Suffice it to say I haven’t definitively made up my mind yet. *sigh*

WOOHOO!!!

25 lbs down! Even though it’s taken me something like 8 months to lost 25 lbs, I’m still really excited about this!

Usually when I try to lose some weight I’ll sign up for the gym and/or weight watchers and begin the ancient song and dance of focusing only on healthy healthy healthy. Inevitably I end up falling off the wagon within a few months and gain all the weight back, and usually a few extra.

This time I decided that while I would make a conscious effort every day to eat healthy (and hopefully get some exercise, although that has admittedly been a lot harder to keep up) I was not going to fall into the same habits as last time. I would allow myself chips or a cookie if I wanted some/it — but I would always exercise portion control. So now, instead of grabbing the bag of ruffles when I’m going to indulge in some chips + dip, I put some in a baggie and when they’re gone, that’s it.

I’m cautiously thinking this approach will work and continue working even after I get to my ultimate goal weight, because I”m allowing for the junk food instead of beating myself up over it. I’m (re-)learning that junk food is okay, as long as you don’t overindulge (like I did for so many years).

Now if only I could push myself to go to the gym!

here's a story, part 2

Yesterday I was bored and reading through some old entries here. I came across “Here’s a story” and remembered what happened the day after I got a date to junior prom, and the true reason he is my “What if Guy”…

A good friend of mine whom I went to school with had been hired and worked with me in housekeeping. We usually carpooled to work on the weekends, and she was friends with Randy, also (she’d heard me talk about him all the time
at school) and would sit with him and me after having a cigarette during break.

On this particular day — which was a few months after I started crushing on him — she came back in and sat with us. Apropo of nothing, she looked at me and said “I heard you got a date to Prom.”

“Yeah, I asked [shithead name redacted] and he said yes,” I replied. I glanced at Randy, who had this look on his face… you know, I can’t really describe it. It wasn’t something cliche like he’d been punched. It was more like a total ‘WTF???’ kind of look.

As my friend and I talked a little more about Prom I kept looking at Randy, who’d become quiet (even though he’d been talking with me quite a bit a few minutes earlier). Eventually  he excused himself and went to the men’s employee lounge. We didn’t talk much that day and it wasn’t until I got home that it came slamming home to me:

I FUCKING BLEW IT!

But (stupid stupid me) thought that it was too late to do anything about it; I’d already asked someone else and they accepted. I started dating this other guy after prom and by summer Randy had quit the hospital to go work at [store].

It could be that I totally misinterpreted the look on his face that day. It could be that he thought of me as nothing more than one of his nice co-workers and nothing more.  I know he had a girlfriend while we worked together; as to whether they were still together by prom time, I don’t remember and neither to my diaries.  We never did anything outside of work.  I don’t know for certain that there were any reciprocated feelings on his end. I don’t know that I’m the reason he quit working at the hosptial; he certainly had other logical reasons for getting a job at [store] — it was closer, probably paid a bit more, and (chances are) people didn’t treat him like shit there like some of the bitches we worked with at the hospital did.

I just remember that look.

Hee

While working on the remove_user function, I have a variable entitled $thread. I need to find the number of rows for the $thread query, and without thinking labeled that one $thread_count. 😀 The kind of humor you would probably only appreciate if you buy your own sheets.