分享

Zend_Amf + flex 数据库连接教程

 顾于思文摘馆 2012-08-18
简单介绍Zend_Amf 与 Flex 实现数据库的操作。flex前端初始化的时候调用getAStudent()方法,该方法驱动mainService下的getAStudent()方 法,后台该方法通过Jd_Model_Student,构建Student对象,并调用其getAStudent()方法,返回数组到 mainService,传输到前端。前端获取到返回数据后,onStudentGot()方法被调用,Alert id为1的学生的name。
在看篇文章前,确定你已经看了这篇文章zend_amf flex前端开发 Hello world 。

1. 数据库的构建
数据库 school
表 student
字段 id, name

2.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.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
 
;database 
resources.db.adapter = "PDO_MYSQL" 
resources.db.isdefaulttableadapter = true 
resources.db.params.driver_options.1002 = "SET NAMES UTF8;" 
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = school
 
[staging : production]
 
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
 
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

3.bootstrap.php文件
在bootstrap文件中需要初始化 autoloader 和 db

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	public function _initDb(){
		$dbAdapter = $this->getPluginResource('db')->init();
		Zend_Registry::set('dbAdapter',$dbAdapter);
	}
 
	public function _initAutoLoader(){
		$autoloader = new Zend_Application_Module_Autoloader(
			array(
				'namespace' => 'Jd',
				'basePath' => realpath(dirname(__FILE__))
			)
		);
 
		return $autoloader;
	}
 
}

4.MainService.php文件

<?php
class MainService{
	public function getAStudent($id){
		$student = new Jd_Model_Student();
		return $student->getAStudent($id);
	}
}

5. Studeng.php文件
该文件位于models文件夹下面,在bootstrap.php文件中的autoloader设置了其namespace为Jd,Model是必须加的。注意该文件名与类名不一样。

<?php
class Jd_Model_Student extends Zend_Db_Table{
	protected $_name = "student";
 
	public function getAStudeng($id){
		$select = $this->select();
		$select->where("id = ?", $id);
		return $this->fetchRow($select)->toArray();
	}
}

6.前端代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="getAStudent(1)">
	<mx:RemoteObject id="mainService" destination="zend_amf" source="MainService">
	    <mx:method name="getAStudent" result="onStudentGot()" fault="onFault(event)" />
	</mx:RemoteObject>
	<mx:Script>
		<![CDATA[
			import mx.rpc.events.FaultEvent;
			import mx.controls.Alert;
			import mx.collections.*;
			private function getAStudent(id:int):void{
				mainService.getAStudent(id);
			}
 
			private function onStudentGot():void{
				var student:Array = new Array();
				student = mainService.getADoc.lastResult;
				Alert.show(student['name']);
			}
		]]>
	</mx:Script>
</mx:Application>


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多