Skip to main content

Connecting Flex 4 with AMFPHP

These days am missing this blog coz am busy with learning Flex. When i tried to connect with AMFPHP i can't find anything useful. Most of them were vague .Using the default data connectivity in Flash Builder am more confused and it's code looks complex . Atlast i found someone helpfull from adobe forum.

When i tried to implement it , it shows nothing and missing some files. When i checked the PHP file, though am not good in PHP , i found there is also something also missing. I tried and searched and corrected the files.
Here is the MXML code:
	
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function getAllEmployees_resultHandler(event: ResultEvent): void {
	dataGrid.dataProvider = event.result;
}
protected function getAllEmployeesByID_resultHandler(event: ResultEvent): void {
	dataGrid.dataProvider = event.result;
}
protected function createEmployees_resultHandler(event: ResultEvent): void {
	Alert.show("inserted with ID=" + event.result);
	EmpService.getAllEmployees();
}
protected function EmpService_faultHandler(event: FaultEvent): void {
	trace(event.fault.faultDetail);
}
protected function datagrid1_creationCompleteHandler(event: FlexEvent): void {
	EmpService.getAllEmployees();
}
protected function Insert_clickHandler(event: MouseEvent): void {
	var e: Employee = new Employee();
	e.first_name = fname.text;
	e.last_name = lname.text;
	e.hire_date = hdate.text;
	e.birth_date = bdate.text;
	e.gender = gender.text;
	e.emp_no = parseInt(empno.text);
	EmpService.createEmployees(e);
}
protected function search_clickHandler(event: MouseEvent): void {
	EmpService.getEmployeesByID(parseInt(stext.text));
}
protected function button1_clickHandler(event: MouseEvent): void {
	EmpService.getAllEmployees();
}
		
This is the PHP service class which needed to be placed in the /services folder both in AMFPHP1.9 and 2.0 versions.
<?php
  include('Employee.php');
  //amfphp1.9
  class EmployeesService
  {
      var $username = "root";
      var $password = "";
      var $server = "localhost";   
      var $port = "3306";
      var $databasename = "fb_tutorial_db";    
      var $tablename = "employees";
      var $connection;
      public function __construct()
      {
          $this->connection = mysqli_connect($this->server, $this->username, $this->password, $this->databasename, $this->port);
      }
      
      public function getAllEmployees()
      {
          $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");
          mysqli_stmt_execute($stmt);
          $rows = array();
		  $row = new Employee();
          mysqli_stmt_bind_result($stmt, $row->emp_no, $row->birth_date, $row->first_name, $row->last_name, $row->gender, $row->hire_date);
          while (mysqli_stmt_fetch($stmt)) {
              $rows[] = $row;
              $row = new Employee();
              mysqli_stmt_bind_result($stmt, $row->emp_no, $row->birth_date, $row->first_name, $row->last_name, $row->gender, $row->hire_date);
          }  
          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
          return $rows;
      }
      
      public function getEmployeesByID($itemID)
      {
          $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where emp_no=?");     
          mysqli_bind_param($stmt, 'i', $itemID);  
          mysqli_stmt_execute($stmt);
		  $row = new Employee();
          mysqli_stmt_bind_result($stmt, $row->emp_no, $row->birth_date, $row->first_name, $row->last_name, $row->gender, $row->hire_date);
          if (mysqli_stmt_fetch($stmt)) {
              return $row;
          } else {
              return null;
          }
      }
      
      public function createEmployees($item)
      {
          $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (birth_date, first_name, last_name, gender, hire_date) VALUES (?, ?, ?, ?, ?)");  
          mysqli_stmt_bind_param($stmt, 'sssss', $item["birth_date"], $item["first_name"], $item["last_name"], $item["gender"], $item["hire_date"]);
          mysqli_stmt_execute($stmt);
          $autoid = mysqli_stmt_insert_id($stmt);
          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
          return $autoid;
      }
  }

Here is the PHP Value Object Class
<?php
class Employee{
public $emp_no;
public $first_name;
public $last_name;
public $gender;
public $hire_date;
public $birth_date;
}
?>
For AMFPHP 2.0 edit the endpoint from http://localhost/amfphp/gateway.php to http://localhost/amfphp2/ (version AmfPHP 2.0 )
And need to change the  createEmployees($item) function in EmployeeService.php to

  
public function createEmployees($item)
      {
          $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (birth_date, first_name, last_name, gender, hire_date) VALUES (?, ?, ?, ?, ?)");  
          mysqli_stmt_bind_param($stmt, 'sssss', $item->birth_date, $item->first_name, $item->last_name, $item->gender, $item->hire_date);
          mysqli_stmt_execute($stmt);
          $autoid = mysqli_stmt_insert_id($stmt);
          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
          return $autoid;
      }
I don't know the reason but it worked for me. But i think in 1.9 the object passed to PHP function is an array and in 2.0 it's an object of stdClass. That's why this change. 

Comments

jason said…
Wow this seems to be the only decent web page on the net that i have found that seems to be doing the same as me if not slightly different, but its not working for me as it inserts the data in the database but with all nulls :(

You have created the object in php where i created it in flash builder!
so i then import the value object into my component and the just use it like you did.

protected function addNewUser():void
{

var ClientData:Clients = new Clients();

ClientData.ClientFname = Fname.text;
ClientData.ClientLname = Lname.text;
ClientData.ClientCountryCodes =comboBoxSlagCountry.selectedItem.CountryCodes;
ClientData.ClientDayofbirth = Day.labelDisplay.text;
ClientData.ClientMonthofbirth = Month.labelDisplay.text;
ClientData.ClientYearofbirth = Year.labelDisplay.text;
ClientData.ClientSex = Sex.labelDisplay.text;
ClientData.ClientTown = Town.text;
ClientData.ClientDescription = Descrip.text;
customerService.createClients(ClientData);
}
and here is my AmfPHP

public function createClients($item) {

$stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (fname, lname, dateofbirth, monthofbirth, yearofbirth, country, town, sex, comments) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'sssssssss', $item->fname, $item->lname, $item->dateofbirth, $item->monthofbirth, $item->yearofbirth, $item->country, $item->town, $item->sex, $item->comments);
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}

as you can see very similar to what you have done but it wont work as just inserts NULLS :(
logicmania said…
@ jason
Thanks for reading my post.
First of all, i am just flash programmer and am not an expert in flex or PHP. And my lap got some problems and lost my project files and installations.. I will try your code ASAP and let you know the result..
In the mean time check your valusobject classes ,see it's in there. You need same valueobject class in PHP and Flex. And also check the version of amfphp. And also check the httpd log.
jason said…
thanks for the quick response logicmania
jason said…
Well i included a class in php for the clients and i thought that would work but it didn't :( and i checked the error.log and there was no errors that i could see and i checked that the class clients was in the valueObjects in flash builder which it was. Its really annoying as when i check in data/services and use test operation it does add the client to my database which would suggest to me that everything after flash builder is working :) but even through we included the class in the php script i cant see us using it to insert the client, but i added it anyway!
jason said…
one last note!

The only difference between your object and mine is that im using GET and SET to get and set the variables!! i wonder if that is what's making the error!
logicmania said…
jason can you send me yor files to me... i will check and revert back you tomorow or day after tomorrow
I think it is one of the better free information available! It’s great to know that brilliant persons just like you are turning to blogging in order to contribute ideas to the people
Duplicator
Promo Disc said…
I have the many of the same thoughts contained in this article. It's good to know I'm not the only person who has these views. You have written an excellent quality informational article. Thank you.
DVD packaging

Popular posts from this blog

Configure PostgreSQL and phpPgAdmin in WAMP

As i told earlier am trying to switch to Flex , i am trying to learn connection between PHP and Flex. When searched i got a excellent tutorial . But the problem is that it uses PostgreSQL. As i don't have any prior experience in PostgreSQL i try to install it in my Windows 7 and configure it in WAMP . I got different errors and spend almost two days in searching and using different methods . Atlast i got it right. I will try to figure out the steps i have done.
Am using
PostgreSQL 9.1WAMP Server 2.0e phpPgAdmin 5.0.2 WAMP consists
1. Apache Version :2.2.17  2. PHP Version :5.3.5  3. MySQL Version :5.5.8 First install PostgreSQL , no need to install additional plugins like phpPgAdmin.
Install WAMP server where ever you need.
Download PhpPgAdmin  and unzip it to WWW folder in WAMP installation.

At this point if you try to access the http://localhost/phpPgAdmin/ you may get this error.

Your PHP installation does not support PostgreSQL. You need to recompile PHP using the --with-pgs…

Angular 4 - Modules, components and routing

In the previous post we created a new Angular 4 project. Lets check the how to add modules, components and routing.

You could check out the code from here.
git checkout -b part2.0 origin/part2.0
We are going to use bootstrap styling . So lets add bootstrap in our aplication . We are going to use only styling not the components.

npm install bootstrap@3
We need to add the bootstrap stylesheet in tha .angular-cli.json.


"styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", "styles.css" ],
Our first Module

Lets add a new module employee. Inside "src/app" run

ng generate module employee or
ng g m employee
Wait we are going to lazy load this module , so we need routing inside the module, right? So we are going to create the routing also.
ng g m employee --routing
Time for component

It will create new employee module with its own routing.lets add a component for listing the employee. So inside employee folder run

ng g c empl…

Angular - 4 year road map

AngularJS is one of the most popular JavaScript frameworks in the web. The Google developed framework is considered as one of the easiest framework to kick start a project. Two way binding and the dependency injection is the highlight of this framework.
My journey with AngularJS started about 4 years ago. Then popular ver1.4 amazed me in its capabilities. Developer like me, coming from Actionscript and plain old JQuery , AngularJS is a good option to kick start a project. It give me the confidence in JavaScript world, and from then I have tried lot of framework and tools from JavaScript itself like requireJS, webpack, etc. I did projects in Angular from 1.4 to the current 2.0, and trying to upgrade to the latest 4. Angular has now become one of the fastest growing framework. The journey starts with 1.4
One of the stable and mostly uses version. Started working on this for small projects and it seems easy for development. Dependency injection and two way binding seems quite interesting. …