Have had some troubles getting data to Flex from PHP using WebORB. Finally got it to work using some workarounds!
The discussion thread is here on the Midnight Coders forums.
We wanted to have classes doing most of our business logic and call them from both PHP as well as WebORB. Flex will make service calls to WebORB to get data in AMF format. The latest version of WebORB has an excellent Service Browser that allows to inspect and invoke services right from a browser console. Since we were including a prepend file in the main class, the Service Browser failed using the file. Had to include the prepend file from the class’s constructor to make it work!
This is because WebORB itself will instantiate an object of the class to invoke a method on it. It can access the $_SESSION variables across all the objects that get instantiated in turn (and not $GLOBALS).
Here’s some PHP code to illustrate:
<?php
class Manager
{
public function __construct()
{
/** IMPORTANT ******
If we do not include the prepend file here, the WebORB Service Browser
tries to load it, and that will create a problem because of PHP reflection
Putting it in the constructor will include the file and make things work.
Also, you can not use $GLOBALS to store variables across functions.
Storing them in $_SESSION will work though.
**/
// The directory this file will get called from is "Consolde" in webroot.
// So include path must be relative to that
$prepend = "../myproject/includes/prepend.inc.php";
if (is_file($prepend)) // sanity check
{
include_once($prepend);
}
}
/**
* Get a list of all favorite products
**/
public function getFavorites()
{
$query = 'SELECT id, name FROM product_favorites
WHERE userId = '.intval($_SESSION['userId']).'
ORDER by lastModified DESC';
$_SESSION['mainDB']->Query($query);
$result = $_SESSION['mainDB']->Result();
return $result;
}
}
?>
And an excerpt of my prepend.inc.php:
$config["dbhost"] = "localhost";
$config["dbuser"] = "user";
$config["dbpass"] = "pass";
$config["dbname"] = "maindb";
$_SESSION["config"] = $config;
// Include all classes for the application
include_once("classes/DB.php");
// Do the DB connection and store it into session
// The DB class will use the $_SESSION["config"] values
// to connect to the DB
$_SESSION["mainDB"] = new DB();
This made it work both from the Service Browser and Flex. It actually also makes it work from the rest of the PHP files I have. Allowing me to test the whole app easily.
This is looking good now!
Thanks!
I’ve tried everything to make this thing work, I know there’s more code but I can’t reach out what it is. Can you give me some lights on it. I’ve tried with the console and every method returns null values.