Sorry, managed to miss that "build up" bit. I'd probably be irritating and suggest that the dbms might do a CSV export, but.....
[unhelpful waffle elided]
If there are any spots where you know you've finished with a big wodge of data, you could try immediately unsetting them. You mention paging the results out a thousand lines a time; could you reduce that to one line, appending to a temporary file?
I am assuming this is a case of PHP's exhausting its memory but it usually triggers a fatal error in that case. It's possible that it's crashing completely, but in that case I'd expect the server to send back a 500 response. (Incidentally, in checking that memory limit message, I tried a test script here; the memory limit is set at 128MB, but PHP didn't quit until it had claimed 500MB and wanted another gig!)
I'm going to have a nosey around Cake to see what it's up to; any suggestions of anything I ought to look at first?
It could also be something up on the database side; can't think what but perhaps you could try emulating your code and running the queries against the database manually.
This sounds like the sort of problem where I'd need to be sitting at the machine to diagnose. Get a feel for it. Bit tricky to do, here 🙂