If I put debug(memory_get_usage()); as the first line of the view, I still get that Fatal Error, which means the problem is not in loops of the view. It doesn't seem to be in the controller either, but rather between the controller and view.

pretty sure you got some infinite look in there somewhere. what do you do before that?
–
markApr 7 '12 at 11:44

If i had infinite loop, it would have never gotten me to the last line, where the debug is. Also, the page sometimes manages to load, and sometimes doesn't. Adding the function as edit so you can see code
–
mgPePeApr 7 '12 at 12:30

1 Answer
1

The most likely reason you're getting this error is because you're working with a lot of data at once, and it's never being released when you're done with it. It's basically an optimisation issue.

Given that you have a limit set to 2000 in there, I'm making the assumption you're working with a large database, and that high value might be the single problem. However, I'll highlight some other things that stood out too.

First, note that you're fetching data from the database in a number of places.

In between all those queries, you're storing a subset of the queries in new arrays, which leads me to believe your model functions return more data than you actually need. Here's one example in your code:

foreach ($cus as $cu){
$uids[]=$cu['EduCourseUser']['user_id'];
}

Here's another one which seems totally and utterly unnecessary, and it's used totally interchangeably with $wt.

$writing['Writing'] = $wt['Writing'];

Finally, you take $wording, and assign it to $wording_sorted, for sorting. I see you pass both to the view, but do you need both sorted and unsorted forms at exactly the same time? I can't tell you what you should do there, but consider your options.

Here's what you can do to help remedy some of this issues:

Destroy references to data when you're done with it: Unset your query results after you've used them, because they're taking up memory for no reason. Here's a couple:

Make sure you're not fetching irrelevant data: It's hard to tell what you're working with, because of the way Cake formats query results, but your models are prime targets for optimisation if the methods within them are returning all the columns in a table, and all its associations, when you only need one or two. Make the conditions you pass to the Cake model more specific if you can.

Use Cake's model count method: You're counting clicks in one place, by fetching them all and using nested iterators. That's a lot of data you don't need when you're just after an integer. Consider creating another model method:

That introduces more database queries, but a counter cache field might come in handy to optimise that.

Make your limit more stringent: your limit is set to 2000. I don't know whether that's really small or really high, but if that means you're showing 2000 results per page, simply cutting that down to less than 100 might solve your problem without even having to do anything else.

As said, this is all optimisation stuff, so your code may not be wrong or broken, merely in need of some fine tuning.