Welcome to Abdul Malik Ikhsan's Blog

Kolaborasi Flex dan zend 1.9.0

Posted in Flex, Tutorial PHP by samsonasik on August 10, 2009

Akhirnya, setelah sekian lama zend 1.8 ke atas dapat dikolaborasikan dengan Flex 3. Untuk yang zend 1.8, yang menjadi masalah bukan di sisi zend-nya, tapi di sisi library amf nya. Bagi yang menggunakan zend 1.8, bisa downgrade ke amf versi 1.7 ( zend tetap pakai 1.8 ), atau upgrade ke zend 1.9.0 sekalian.

Struktur directory framework untuk kebutuhan koneksi ke zend 1.9.0 berbeda dari sebelumnya, dapat dilihat dari gambar berikut :

Di sini kita tidak butuh controller, karena sudah digantikan oleh service.

Isi file configs\application.ini :

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH “/../library”
bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
bootstrap.class = “Bootstrap”
resources.db.adapter = “PDO_MYSQL”
resources.db.params.host = “localhost”
resources.db.params.username = “root”
resources.db.params.password = “”
resources.db.params.dbname = “test”

[staging : production]
resources.db.params.dbname = “test”

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.params.dbname = “test”

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.params.dbname = “test”

Untuk Bootstrap file :
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

protected function _initAutoload()
{

$autoloader = new Zend_Application_Module_Autoloader(array(

‘namespace’ => ”,

‘basePath’ => dirname(__FILE__),

));

return $autoloader;
}

}

Untuk contoh isi file models : (asumsi ada table bernama testaa, field-fieldnya id dan nama)
<?php
class Model_Testaa extends Zend_Db_Table
{
protected $_name = “testaa”;
protected $_primary = “id”;
}

Untuk penjabaran field-field table, kita definisikan di class TestaaClass (contoh) di directory services :
<?php
class TestaaClass
{
var $id ;
var $nama;
}

Nah, untuk servicenya :
<?php
require_once(‘TestaaClass.php’);

class TestaaService {

private $testaa;

public function __construct()
{
$this->testaa = new Model_Testaa();
}

public function getData()
{

$result = array();
foreach($this->testaa->fetchAll() as $key=>$val)
{

$testaaobj = new TestaaClass();
$testaaobj->id = $val->id;
$testaaobj->nama = $val->nama;

$result[] = $testaaobj;
}
return $result;
}

public function removeData($idField)
{
$currentTestaa = $this->testaa->find($idField)->current();
$currentTestaa->delete();
}

public function addData($namanya)
{

$data = array(‘nama’ =>$namanya);
$this->testaa->insert($data);
}

public function editData($id, $namanya)
{

$row = $this->testaa->find($id)->current();
$row->nama = $namanya;
$row->save();
}
}

Untuk index.php :

<?php

defined(‘APPLICATION_PATH’) || define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/../application’));
defined(‘APPLICATION_ENV’) || define(‘APPLICATION_ENV’, (getenv(‘APPLICATION_ENV’) ? getenv(‘APPLICATION_ENV’) : ‘production’));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . ‘/../library’),
get_include_path(),
)));

require_once ‘Zend/Loader/Autoloader.php’;
$autoloader = Zend_Loader_Autoloader::getInstance();

$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . ‘/configs/application.ini’
);

$application->bootstrap();

$server = new Zend_Amf_Server();
$server->addDirectory(‘./services/’);
echo($server->handle());

Nah, sekarang, untuk di flex nya, struktur nya sama seperti pada dokumentasi sebelumnya :

Jangan lupa setting compiler settingnya :

Untuk konfigurasi service-config.xml nya :
<?xml version=”1.0″ encoding=”UTF-8″?>

<services-config>

<services>

<service id=”amfphp-flashremoting-service” class=”flex.messaging.services.RemotingService” messageTypes=”flex.messaging.messages.RemotingMessage”>

<destination id=”zendamf”>

<channels>

<channel ref=”myamfphp”/>

</channels>

<properties>

<source>*</source>

</properties>

</destination>

</service>

</services>

<channels>

<channel-definition id=”myamfphp” class=”mx.messaging.channels.AMFChannel”>

<endpoint uri=”../zf190_amf/public/index.php” class=”flex.messaging.endpoints.AMFEndpoint”/>

</channel-definition>

</channels>

</services-config>

Terakhir, isi source mxml kita :

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”vertical”

creationComplete=”getDataExample()”>

<mx:RemoteObject id=”remoteExample” destination=”zendamf” source=”TestaaService”

showBusyCursor=”true” fault=”faultListener(event)”>

<mx:method name=”getData” result=”getDataListener(event)”/>

</mx:RemoteObject>

<mx:Script>

<![CDATA[

import mx.controls.Alert;

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

import mx.collections.ArrayCollection;

//siapkan array

[Bindable]

private var exampledata :ArrayCollection;

[Bindable]

private var idnya :Number;

private function getDataListener(event:ResultEvent):void

{

exampledata = new ArrayCollection(event.result as Array) ;

}

private function faultListener(event:FaultEvent):void

{

Alert.show(event.fault.message, “Error”);

}

private function getDataExample():void

{

//load data

remoteExample.getData();

}

]]>

</mx:Script>

<mx:DataGrid id=”myGrid” dataProvider=”{exampledata}”>

<mx:columns>

<mx:DataGridColumn headerText=”id” dataField=”id” draggable=”true” />

<mx:DataGridColumn headerText=”nama” dataField=”nama” draggable=”true”/>

</mx:columns>

</mx:DataGrid>

</mx:Application>

Hasil akhirnya :

Kalau dicoba seperti ini sudah jalan, berarti kita sudah berhasil 🙂

Tagged with:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: