|
原文同时发布于http://hi.baidu.com/gdutpxz/blog ... f9773a8694732f.html
在flex cookbook上的amfphp例子中,可以看到作者并没有使用到Employee.php和Employee.as 这两个文件
这里给出一个使用这两个文件的例子。
由于没有安装Oracle ,数据库使用的是MySql。
使用自定义类(custom class) 时最重要的只有两点
1.php版本的类里的$_explicitType 变量
2.as版里的 [RemoteClass] 元
一般情况下这些都是ValueObject,据flexbuilder带来的说明,序列化时仅对两者的属性进行,当然,反序列化也只能结属性执行了。
下面是一个不依赖 -services xxx.xml 编译参数的例子:
===========================================================
客户端源代码开始
===========================================================
========================================================
源文件 main.mxml 如果你的amfphp是放在其它目录,改掉红色部分。
========================================================<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()" layout="absolute">
<!--
这个RemoteObject调用EmployeeService。它包含有:destination, source以及事件处理。(译注:destination与service-config.xml的destination对应,而source则与具体服务名称相关)
-->
<mx:RemoteObject endpoint="http://localhost/amfphp/gateway.php" id="myservice" source="Project.EmployeeService" destination="amfphp" fault="faultHandler(event)" showBusyCursor="true">
<mx:method name="getEmployees" result="resultHandler(event)"
fault="faultHandler(event)">
</mx:method>
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import project.Employee;
import mx.utils.ArrayUtil;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
//当数据返回里dp:ArrayCollection被设为服务端返回的ResultEvent的result属性的内容。
[Bindable]
private var dp:ArrayCollection;
public function init():void{
}
private function faultHandler(event:FaultEvent):void {
Alert.show(event.fault.faultString, event.fault.faultCode.toString());
}
private function resultHandler(event:ResultEvent):void {
dp=new ArrayCollection(ArrayUtil.toArray(event.result));
try{
var temp:Employee = dp[1] as Employee;
Alert.show("Emaile="+temp.EMAIL);
}catch(e:Error){
try{
var temp2:Employee=event.result as Employee;
Alert.show("temp2 Emaile="+temp2.EMAIL);
}catch(ee:Error){
Alert.show("What cow second..");
}
}
}
]]>
</mx:Script>
<mx:Canvas x="0" y="0" width="100%" height="100%">
<mx:Button x="10" y="10" label="Get data"
click="myservice.getOperation('getEmployees').send()"/>
<!--
每一列都与服务端返回相关联
-->
<mx:DataGrid x="10" y="40" width="100%" height="100%" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="EMPLOYEE_ID" dataField="EMPLOYEE_ID"/>
<mx:DataGridColumn headerText="FIRST_NAME" dataField="FIRST_NAME"/>
<mx:DataGridColumn headerText="LAST_NAME" dataField="LAST_NAME"/>
<mx:DataGridColumn headerText="EMAIL" dataField="EMAIL"/>
<mx:DataGridColumn headerText="PHONE_NUMBER"
dataField="PHONE_NUMBER"/>
<mx:DataGridColumn headerText="HIRE_DATE" dataField="HIRE_DATE"/>
<mx:DataGridColumn headerText="JOB_ID" dataField="JOB_ID"/>
<mx:DataGridColumn headerText="SALARY" dataField="SALARY"/>
<mx:DataGridColumn headerText="COMMISSION_PCT"
dataField="COMMISSION_PCT"/>
<mx:DataGridColumn headerText="MANAGER_ID" dataField="MANAGER_ID"/>
<mx:DataGridColumn headerText="DEPARTMENT_ID"
dataField="DEPARTMENT_ID"/>
</mx:columns>
</mx:DataGrid>
</mx:Canvas>
</mx:Application>
==========================================================
源文件: project/Employee.as (注意大小写)
==========================================================
- package project {
- [RemoteClass(alias="Project.Employee")]
- [Bindable]
- public class Employee
- {
- public var EMPLOYEE_ID:Number;
- public var FIRST_NAME:String;
- public var LAST_NAME:String;
- public var EMAIL:String;
- public var PHONE_NUMBER:Number;
- public var HIRE_DATE:String;
- public var JOB_ID:Number;
- public var SALARY:Number;
- public var COMMISSION_PCT:Number;
- public var MANAGER_ID:Number;
- public var DEPARTMENT_ID:Number;
- //public function Employee(){}
- }
- }
复制代码 ===========================================================
客户端源代码结束
===========================================================
服务端源代码开始
===========================================================
===========================================================
PHP源文件 services_path/EmployeeService.php
===========================================================
- <?php require_once('./Employee.php');
- class EmployeeService {
- function getEmployees(){
- $myconnection = mysql_connect("localhost","hr","hr");
- mysql_select_db("fcb_Employee");
- # 检查MySql链接
- if (!myconnection) {
- # 链接失败,不简单地使用die而是返回一些有用的数据给客户端。
- trigger_error("AMFPHP Remoting 'EmployeeService'
- class could not connect: " . mysql_error());
- }
- $query="SELECT * FROM employee";
- # 返回雇员列表
- $result=mysql_query($query);
- if (!$result) {
- trigger_error("AMFPHP Remoting 'EmployeeService'
- class database SELECT query error: " . mysql_error());
- }
- while ($row = mysql_fetch_assoc($result)) {
- $tmp=new Employee($row);
- $data_array[] = $tmp;
- }
- return $data_array;
- }
- }
- ?>
复制代码 ===========================================================
PHP源文件 services_path/Project/Employee.php (注意大小写)
===========================================================
- <?php class Employee
- {
- var $EMPLOYEE_ID=1;
- var $FIRST_NAME="a";
- var $LAST_NAME="b";
- var $EMAIL="[email protected]";
- var $PHONE_NUMBER=3;
- var $HIRE_DATE="2009";
- var $JOB_ID=3;
- var $SALARY=9999;
- var $COMMISSION_PCT=43;
- var $MANAGER_ID=4;
- var $DEPARTMENT_ID=6;
- var $_explicitType = "Project.Employee";
- function Employee($r){
- $this->init($r);
- }
- function init($r){
- $keys=array_keys($r);
- foreach($keys as $k){
- $this->$k=$r[$k];
- }
- }
- }
- ?>
复制代码 ======================= 数据库部分 ===========================
================== 请以有创建用户权限的帐号导入========================
- create database fcb_Employee;
- use fcb_Employee;
- --
- -- 表的结构 `employee`
- --
- CREATE TABLE IF NOT EXISTS `employee` (
- `EMPLOYEE_ID` int(10) unsigned NOT NULL auto_increment,
- `FIRST_NAME` varchar(20) NOT NULL,
- `LAST_NAME` varchar(20) NOT NULL,
- `EMAIL` varchar(50) NOT NULL,
- `PHONE_NUMBER` varchar(20) NOT NULL,
- `HIRE_DATE` date NOT NULL,
- `JOB_ID` int(10) NOT NULL,
- `SALARY` int(10) NOT NULL,
- `COMMISSION_PCT` int(10) NOT NULL,
- `MANAGER_ID` int(10) NOT NULL,
- `DEPARTMENT_ID` int(10) NOT NULL,
- PRIMARY KEY (`EMPLOYEE_ID`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
- GRANT ALL PRIVILEGES ON fcb_Employee.* TO hr@localhost IDENTIFIED BY 'hr' WITH GRANT OPTION;
- -- 插入数据,为了省事,这两段数据是ctrl+c ctrl+v得到的...仅有ID不同..
- INSERT INTO `fcb_Employee`.`employee` (
- `EMPLOYEE_ID` ,
- `FIRST_NAME` ,
- `LAST_NAME` ,
- `EMAIL` ,
- `PHONE_NUMBER` ,
- `HIRE_DATE` ,
- `JOB_ID` ,
- `SALARY` ,
- `COMMISSION_PCT` ,
- `MANAGER_ID` ,
- `DEPARTMENT_ID`
- )
- VALUES (
- NULL , 'Pan', 'iptton', 'iptton#gmail.com', '999999999', '2008-06-10', '1', '999999', '999', '9', '9'
- );
- INSERT INTO `fcb_Employee`.`employee` (
- `EMPLOYEE_ID` ,
- `FIRST_NAME` ,
- `LAST_NAME` ,
- `EMAIL` ,
- `PHONE_NUMBER` ,
- `HIRE_DATE` ,
- `JOB_ID` ,
- `SALARY` ,
- `COMMISSION_PCT` ,
- `MANAGER_ID` ,
- `DEPARTMENT_ID`
- )
- VALUES (
- NULL , 'Pan', 'iptton', 'iptton#gmail.com', '999999999', '2008-06-10', '1', '999999', '999', '9', '9'
- );
复制代码
[ 本帖最后由 onttpi 于 2008-6-10 22:48 编辑 ] |
评分
-
3
查看全部评分
-
|