Introduction(介绍)
AS3FlexDB project is a open source library that allows Adobe Flex applications to connect to a MySQL server. This library use AMFPHP to access a MySQL server.
AS3FlexDB项目是一个开源类库,允许Adobe Flex应用程序连接MySQL服务器,这个类库使用AMFPHP访问MySQL服务器
AS3FlexDB let you write all your SQL's in FLEX and not in PHP. More exactly you can do almost every action on a table in FLEX without making new classes in PHP and access it.
AS3FlexDB让你在FLEX中写所有的SQL语句而不是在PHP中。更准确的说是你可以在FLEX中对一个表做几乎所有的动作而不需要在PHP建立新的类并访问这个类
From version 1.4.0 AS3FlexDB can bind a table with a Flex Component just from MXML.
AS3FlexDB的1.4.0版本可以使用MXML的一个FLEX组件绑定一个表
Download AS3FlexDB(下载AS3FlexDB)
You can download AS3FlexDB from here.
你可以从这里下载AS3FlexDB
Setup AMFPHP(安装AMFPHP)
安装AMFPHP
First download and install AMFPHP. After you have installed AMFPHP copy "AS3FlexDBKit/src/FlexDB/services/database.php" file from AS3FlexDB archive in your amfphp services folder. (ex: amfphp/services/mysql/database.php)
首先下载并安装AMFPHP。在你安装AMFPHP后,从AS3FlexDB文件夹中复制"AS3FlexDBKit/src/FlexDB/services/database.php"到你的AMFPHP中的services文件夹(例如:amfphp/services/mysql/database.php)
Prepare Adobe Flex Builder(在Adobe Flex Builder中开始)
Create a new Flex Project: "FlexDBExample".
Copy as3flexdb_version.swc file from "AS3FlexDB-version.zip/bin" folder into your "libs" folder.
Copy services-config.xml from FlexDB-version.zip/src/FlexDBTest/ folder into your project folder.
创建一个新Flex项目:” FlexDBExample”
从"AS3FlexDB-version.zip/bin"文件夹复制as3flexdb_version.swc到你的”libs”文件夹
从FlexDB-version.zip/src/FlexDBTest/文件夹复制services-config.xml到你的项目文件夹.
If you done everything ok you should have:
如果你完成了所有事情,你应该有如下文件
bin-debug [folder]
libs
as3flexdb_1.3.0.swc
FlexDBExample.mxml
services-config.xml
4. Setup service-config.xml(设置service-config.xml)
Open services-config.xml
Search for "http://localhost/amfphp/gateway.php" and replace this with your link to gateway.php file
打开services-config.xml找到"http://localhost/amfphp/gateway.php",并将它替换成你gateway.php文件的访问链接
Right click on your Flex Project and in Flex Compiler textinput paste -services "services-config.xml" and click ok
右击你的Flex项目,在Flex Compiler中的文本框中增加 -services "services-config.xml",然后点击OK
5. Add a datagrid and connect to MySQL(增加一个datagrid并连接MySQL)
First of all let’s add a datagrid to our application.This is the code:
首先让我们增加一个datagrid到我们的应用程序中。以下是代码
<mx:DataGrid id="dg1" width="100%" height="100%" dataProvider="{q1.Records}">
<mx:columns>
<mx:DataGridColumn dataField="fname" headerText="First Name"/>
<mx:DataGridColumn dataField="lname" headerText="Last Name"/>
<mx:DataGridColumn dataField="password" headerText="Password"/>
</mx:columns>
</mx:DataGrid>
Now you must define phi namespace (xmlns:phi="phi.db.*") for you to use AS3FlexDB classes in your MXML. This is the code:
现在为了在你的MXML中使用AS3FlexDB,必须定义phi这个名字空间(xmlns:phi="phi.db.*")。以下是代码
<mx:Application layout="vertical" xmlns:phi="phi.db.*" xmlns:mx="http://www.adobe.com/2006/mxml">
Now all you have to do, is to make a Database object set user, pass, db, and host correctly and bind a Flex Component (ex: ComboBox, DataGrid) with a Query object.
现在你所要做的,创建一个数据库对象(Database object),正确设置用户,密码,数据库以及主机,然后通过一个查询对象(Query object)绑定一个Flex组建(例如:ComboBox, DataGrid)
<phi:ConnectionData id="c1" name="mxml_conn1" host="localhost" db="test" username="root" password="root" />
<phi:Database id="db1" connection="{c1}" /> // This is the connection with database这里是与数据库连接
// This is the query object. You can use the id to bind a Flex
// component with an SQL.
这里是查询对象(query object),你可以使用id去绑定一个Flex组建,它带有SQL语句
<phi:Query id="q1" database="{db1}" q="SELECT * FROM users WHERE 1" />
<phi:QueryExecute id="q1ex" query="{q1}" />
I have create a table "users" for test. So this example will query our database and select all users in this table.
The table has this structure:
我为测试创建了”users”表。所以这个例子将查询我们的数据库并选择出这个表中的所有用户
这个表有如下结构:
CREATE TABLE `users` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`fname` VARCHAR( 255 ) NOT NULL ,
`lname` VARCHAR( 255 ) NOT NULL
) ENGINE = innodb;
And this is all!
以上就是全部过程
6. Download(下载)
Download files used for this tutorial here
你可以从这里下载这个教程中所有要用到的文件