Overview
This plugin makes possible to connect symfony as backend with adobe flash, and can generated php class services that can be used from Adobe Flash Builder 4. Basically it generates php class that loads the symfony project from the outside ,like external library and uses it's features , then Zend Amf library parses our php class and what return it's methods , convert returned if is array of symfony orm specific objects to array of strings , integers ... things that Zend amf understands.
And use generate services of this plugin just like the plain php generated services from Flash Builder 4. For now it has only Propel orm support.
Installation
First run plugin install command to install plugin.
symfony plugin:install zAmfPlugin --stability=alpha
Clear cache
symfony cc
Usage
generating name of service.php file
First you must generate php class service.
symfony zamf:generate-service [name of service] [name of Model]
name of service is name of class/phpfile generated is required argument.
name of model is the name of database table from what we'll generate
service to work with and to get items , add, delete , update....
!!!They cannot be the same.!!!
the code generate should look like this
<?php
class test4 extends sfZAmf{
public function __construct()
{
parent::__construct();
sfConfig::set("debug",true);
}
public function getAllTests() {
$c = new Criteria();
$tests = $this->map(TestsPeer::doSelectJoinAll($c));
return $tests;
}
public function getAllUsers() {
$c = new Criteria();
$users = UsersPeer::doSelect($c);
return $users;
}
public function getCount() {
$count = TestsPeer::doCount(new Criteria());
return $count;
}
public function getTestsPaged($ofset,$limit) {
$c = new Criteria();
$c->setOffset($ofset);
$c->setLimit($limit);
$tests = TestsPeer::doSelect($c);
return $tests;
}
public function getTests($amfObj) {
$tests = TestsPeer::retrieveByPk($amfObj->Id);
return $tests;
}
public function getTestsById($id) {
$tests = TestsPeer::retrieveByPk($id);
return $tests;
}
public function updateTests($amfObj) {
$tests = TestsPeer::retrieveByPk($amfObj->Id);
if($tests!=null)
{
$this->update($tests,$amfObj);
$tests->save();
}
}
public function createTests($amfObj) {
$tests = new Tests();
$this->create($tests,$amfObj);
}
public function deleteTests($amfObj) {
$tests = TestsPeer::retrieveByPk($amfObj->Id);
if($tests!=null)
$tests->delete();
}
}
generating of amf_config.ini file
After that, we need to generate configuration file that points to Zend amf library, included in this plugin (Zend lib have little modifications to work with symfony)
symfony zamf:generate-config
All generated files can be found in project_root/services folder.
watch video tutorial
And we are ready to proceed with this tutorial: Write Flex and PHP code using Flash Builder 4
place amf_config.ini file in flex project debug folder
When you start new Flex project and specify server type = php and then specify server url and path.
On finish new folder is created in specified server root path ( some path......\htdocs) , with name your flex project name-debug.
For example if web route directory is
c:\xampp\htdocs
and your flex project name is
test
The folder to look to paste the config file generated by plugin is
c:\xampp\htdocs\test-debug
connect to data service
Then only after put that config file you can connect with data services , generated by this plugin.
(services are in project_root/services folder )
from here you can follow video tutorial
BTW.
When you detect auto return type of method, method should return something - an array form that to detect datatypes.
In other words if we auto detect returned of getAllTests method ,
we should add some test information in tests database.
Debugging
One way to debug your code by open url of service in browser,
uncoment
require dirname(__FILE__).'/../lib/sfZAmf.class.php';
and use
public function getAllTests() {
$c = new Criteria();
$tests = TestsPeer::doSelectJoinAll($c);
print_r($this->map($tests));
return $tests;
}
The method
$this->map();
convert array of propel objects to array with strings , int , booleans ....
Another way is to test what method returns and you can also follow this tutorial
Debug Flex and PHP code using Flash Builder 4
Features:
Byte array support
This plugin contain zend amf lib , so has all of it features , as byte array support ...
Flex >> Php
Flex code
var ba:ByteArray = bmp.getBytes();
ba.compress();
service.saveBitmap(ba);
Php code
function saveBitmap($ba){
$data = $ba->data;
$data = gzuncompress($data);
file_put_contents("rawdata.rgba", $data);
return true;
}
and the Php >> Flex
function getSwf(){
return new ByteArray(file_get_contents("my.swf"));
}
ArrayCollection support
The service can return array of strings , int, booleans ...
public function getImage() {
$values = array();
$values[] = ...;
$values[] = ...;
return $values;
}
Orm support
It also has Propel orm support.
This mean that you can return an array of Propel objects and flash'll show data of that objects.
public function getAllTests() {
$c = new Criteria();
$tests = TestsPeer::doSelectJoinAll($c);
return $tests;
}
Even more , if there are foreign keys in table from what , are the objects , it'll return and the data from foreign table.
Array
(
[0] => Array
(
[Id] => 1
[UsersId] => 1
[Name] => test1
[Users] => Array
(
[Id] => 1
[Name] => test
)
)
To use show data from foreign table in flex use something like this
Users.Name
<mx:DataGrid x="35" y="36" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getAllTestsResult.lastResult}">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="Name"/>
<mx:DataGridColumn headerText="Users" dataField="Users.Name"/>
<mx:DataGridColumn headerText="Id" dataField="Id"/>
<mx:DataGridColumn headerText="UsersId" dataField="UsersId"/>
</mx:columns>
</mx:DataGrid>
Even more you can specify referencing tables to be in output , the plugin provides
$this->map([array of Orm objects ],[array with tables to be used]);
method that says which referencing tables data to be i output.
For example
$this->map(TestPeer::doSelectJoinAll(new Criteria),array("Users"));
will return
Array
(
[0] => Array
(
[Id] => 1
[UsersId] => 1
[Name] => test1
[Users] => Array
(
[Id] => 1
[Name] => test
)
)....
and
$this->map(TestPeer::doSelectJoinAll(new Criteria),false);
will restrict to not use data from foreign tables.
Array
(
[0] => Array
(
[Id] => 1
[UsersId] => 1
[Name] => test1
)....
The need of this method is because of that is unpossible to now when there is doSelecJoinAll and when is regurar doSelect.
Easy create/update objects
There are two functions that replaces , setting values for all fields in a table.
Code like this
$tests = new Tests();
$tests->setName($amfObj->Name);
$tests->setTitle($amfObj->Title);
$tests->setDescription($amfObj->Description);
$tests->setDate($amfObj->Date);
$tests->save();
now looks like this
$tests = new Tests();
$this->create($tests,$amfObj);
$tests->save();
The difference between create and update method is that create does not sets Primary keys fields.