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

Upload Video to Facebook Using Actionscrpt 3.0

Angular4 - primeng, ngx-bootstrap

Configure PostgreSQL and phpPgAdmin in WAMP

Save image from Flash using Node.js and Socket.io

Flash FLV player using PHP