CakePHP + AMFPHP + Flex
Introduction
There's been a big buzz over Adobe's new product Flex. Flash9 hasn't even come out yet (at the date of this tutorial), but people are already hard at work on Flex/AS3 applications. However, there is still a lot of ground to be covered and things that are slight mysteries.
Figuring out how to use Flex for my CakePHP applications was one such mystery. Mike Potter at Adobe posted a very nice short and sweet tutorial on getting Flex to work with PHP via AMFPHP remoting (AMF0).
I also knew that CakePHP worked well with AMFPHP under Actionscript 2. There is a nice tutorial on doing this for a simple bulletin board application.
Since the popular bulletin board example, there has been a new version in the works that upgrades that example to AMFPHP 1.9 and uses AMF3 encoding. I have taken that upgrade and applied it so my example here also works in AMF3. Though this specific example is not AMF3. I will try to upload a new version soon. I doubt you'll notice the difference though. However the important thing to note is with the new version of AMFPHP 1.9 in use, a server extension can be used to encode/decode data natively. According to some benchmarks, this increases speed significantly.
Sounds good right? Just follow the tutorials and make it all work. Well, unfortunately there's a few cathces. Flex has some wonderful tools and the DataGrid certainly is one of them. It's essentially a table, and this table can be filled automatically by a data provider that gets information from a database, xml file, etc.
This is super powerful. It has its place in many applications of course. The problem is the data that comes out of CakePHP through AMFPHP (or cakeamfphp, a modified version of AMFPHP) isn't compatible with the DataGrid by default.
You have to make a new function inside CakePHP (a custom query in your Model for your Controller to use) to gather/prepare the data that gets sent to Flex. CakePHP's arrays are too complex for Flex's DataGrid. They are about one level too deep if you only return one model/table at a time. You're even worse off yet if you run a findAll() on a Model that has others associated with it.
This isn't to say Flex can't accept the complex array, it just means the DataGrid can't use it as a data provider. You can still step through the complex array in Flex and assign text areas values from the array. The following pages here will show you the code you need to gather data from CakePHP for use in Flex's DataGrid.