CakePHP + AMFPHP + Flex
Notes/Tutorial Here
So the big hype right now is Flex and it's definitely something I want to learn myself moving forward. It would be very good for any PHP developer to learn this as then they can easily use Flex (or Flash) with their projects. Especially because I can use it in Eclipse with my othe projects (an already familiar environment to me that also saves me from needing to run another application). However, I don't want to abandon everything I've been doing with PHP. I can live without Javascript (or at least heavy javascript effect libraries) for now. I love them, but they have a good bit of overhead and Flash is simply more advanced.
So Flex was the answer...especially because I can integrate it with my CakePHP applications. A lot of people have been wondering about it and a few people have even been working on some interesting things with CakePHP and Flex (thanks gwoo for tipping me off to the SVN of that project you were working on). So I took my stab at things.
There exists no real tutorial in how to do this yet. There is a wonderful article on AMFPHP and CakePHP actually linked on the www.cakephp.org homepage. This is a great place to start and it includes a vendor for CakePHP called "cakeamfphp" -- while there is a new version in the works, I'm actually getting away with version 0.60.
I'm also using AMF0 and not AMF3. This is something that may change in the future as I learn more and as I run into the demand for AMF3. I'm sure it's fancier, faster, and all that.
However, the solution I've come up with is worlds shorter than what I've been seeing people try.
It's a mix between the CakePHP AMF Bulletin Boards example and Mike Potter's Flex and AMFPHP example available on Adobe's site.
The one thing I dislike about Flex is that it needs the data handed to it in a more simple level than CakePHP uses. Well, it doesn't, you can access the same detailed array structures that CakePHP uses but the DataGrid can't. More to the point, if you read Mike Potter's tutorial on AMFPHP and Flex, you'll see that he actually passes data from the MySQL database in a special way as to pass objects.
One of the first problems I ran into while trying to take Mike Potter's tutorial to CakePHP was flags about trying to convert objects to arrays, etc. Then after getting the data to pass, I ran the debugger and saw how Flex was getting the data.
It was an array of objects and more specifically, a multidimensional array of objects. It had [0] [Model] [Column] ... of course this is because with CakePHP, running a findAll() returns all the associated models as well. That's no good for Flex's DataGrid. I could specify the dataprovider to Flex as array[0][Model] and it would sure enough use all the columns and populate the DataGrid...BUT only with the first entry and not all of the entries.
In other words, I had to step out a level. The array was too deep. So that means [0] [Model] [Column] had to become [Model] [Column] ... more specifically [0] [Column].
So I ran Mike Potter's simple query to return an array structure like that. This sort of abandons Cake's default functions for returning data from your database, but I suppose that's the price to pay.
It's not bad though. You simply make your own "findAll()" for JUST the index type pages. You can still use findAll(), find(), findNeighbours(), generateList(), etc. for getting data to use elsewhere inside Flex, but you will just have to run other logic in Flex and you probably won't be using the DataGrid at that rate.
I'm at work, but I will post a full tutorial on how to get CakePHP, AMFPHP, and Flex working. However, if you're an advanced user you will probably be able to figure it out based on what I just explained. This post makes a good introduction or overview to the tutorial I will post soon.
Other Blogs