What I found useful was to make an ArrayCollection class in PHP like the
following (note: I didn't come up with this idea, but it works great, but I
cannot find the link where I found this)
 
class ArrayCollection extends ArrayObject 
{ 
  var $_explicitType = "flex.messaging.io.ArrayCollection"; 
 
  public function __construct($config = array()) 
  { 
    // Allow accessing properties as either array keys or object properties:

    parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); 
  } 
} 
 
Then, when loading a mysql response from mysql_fetch_array:
 
$myrow = mysql_fetch_array();
$Result = new ArrayCollection($myrow); 
 
Once it sends it back to Flex, Flex will automatically see it as an array
collection -- without any special mapping or anything.  Works great to
populate datagrids, with any strongly typed classes.  I believe this
requires > PHP 5 but I'm not entirely sure.
 
I prefer this so as I'm changing what's coming from the DB, I'm not updating
2 additional classes (one in PHP, the other in Flex).


 

  _____  

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Daniel Tse
Sent: Tuesday, March 25, 2008 2:55 AM
To: [email protected]
Subject: Re: [flexcoders] Re: AMFPHP MySQL Results Object



Thanks Aaron,

I'll see what I can look up on the RemoteObject as well. 

CaffeineWabbit,

Thanks for the pointers. I saw that Mike Potter's AMFPHP example also
created a php array instead of using the mysql results object but... I
noticed on the AMFPHP browser page it can actually create a table based on
the recordset that is returned. e.g. the table automatically changes based
on my sql result and it doesn't seem like there's any specific binding to
columns. I'm curious as to how they did that.

In short, I'd like to find a convenient way for the client to parse the
results of a remote sql query. 

Thanks all!
-Daniel



On Mon, Mar 24, 2008 at 5:53 PM, Aaron Miller <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> eractive.com> wrote:


Just as a follow up. I verified that using RemoteObject does in fact return
an ArrayCollection as the result, while NetConnection does not (returns an
outdated ResultSet object). Hopefully someone at Adobe could explain the
difference in remoting procedure between the two methods.

Regards,
~Aaron




On Mon, Mar 24, 2008 at 5:23 PM, Aaron Miller <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> eractive.com> wrote:


Hello again. I did some more reading on this and I think the problem is with
using NetConnection instead of RemoteObject. Apperently, using NetConnection
does not always send the right headers to tell AMFPHP to use AMF3 encoding,
thus it tries to return a ResultSet instead of an ArrayCollection. You can
tell you got a ResultSet if the return object has a serverInfo property.
>From what I've read, using RemoteObject is a surefire way to get the right
headers (AMF3) and will guarantee an ArrayCollection is returned. However,
having been accustomed to NetConnection myself, and I am trying to figure
out a way to do this without RemoteObject. Setting
NetConnection.objectEncoding to ObjectEncoding.AMF3 did not help for some
reason. Maybe someone else can chime in on this.

In  the mean time, here is a good tutorial on RemoteObject with AMFPHP:

http://www.sephirot
<http://www.sephiroth.it/tutorials/flashPHP/flex_remoteobject/index.php>
h.it/tutorials/flashPHP/flex_remoteobject/index.php

Best Regards,
~Aaron 



On Mon, Mar 24, 2008 at 3:57 PM, caffeinewabbit <caffeinewabbit@
<mailto:[EMAIL PROTECTED]> earthlink.net> wrote:


Hi Daniel,

In the PHP code you provided, you can't pass back the raw $results
variable because its not a normal PHP variable - its a resource, which
holds external data and can only be used by special PHP functions (in
this case, the mysql library.)

To pass back the results from your query, you'll first need to extract
the data into a format that can be passed by AMFPHP. Something similar
to the following:

<?php



class SimplePerson 
{
function getPeople() 
{
$mysql = mysql_connect(localhost, "root", "root");

mysql_select_db( "people-test" );

$sSQL = "SELECT * FROM `tblPeople`";

$results = mysql_query($sSQL);


$queryResults = array();

while($queryOb = mysql_fetch_assoc($results))
$queryResults[] = $queryOb;

return $queryResults;
}
}

?>

That'll show up in Flex as an array containing generic objects that
contain your query results.

For more info on resources, this link should help:
http://www.php. <http://www.php.net/manual/en/language.types.resource.php>
net/manual/en/language.types.resource.php

Hope this helps! 


--- In [EMAIL PROTECTED] <mailto:flexcoders%40yahoogroups.com> ups.com,
"Daniel Tse" <[EMAIL PROTECTED]> wrote:
>
> Hi All,
> 
> I have setup AMFPHP and it returns back the recordset object from a
MySQL
> query. I was wondering how I access that array/recordset from the
flex side.
> 
> e.g. I have the following:
> 
> <?php
> //In the services directory of AMFPHP
> class SimplePerson {
> function getPeople() {
> $mysql = mysql_connect(localhost, "root", "root");
> 
> mysql_select_db( "people-test" );
> 
> $sSQL = "SELECT * FROM `tblPeople`";
> 
> $results = mysql_query($sSQL);
> 
> return $results;
> }
> }
> 
> ?>
> 
> In Flex..
> public function getPeople():void {
> myService.connect(REMOTESERVERURL);
> 
> var responder:Responder = new
Responder(getPeople_Result,
> onFault);
> myService.call("SimplePerson.getPeople", responder);
> 
> }
> 
> public function
> getPeople_Result(aoResults:WHAT_SHOULD_GO_HERE):void
> {
> //test;
> //I'd like to access the result set e.g. iterate
through the
> rows looking at certain columns
> }
> 
> "Array" doesn't seem to work (the fact that it's a 1-dimensional array
> doesn't help)
> "XMLList" the object doesn't seem to map like that either
> 
> Am I missing some kind of object?
> 
> Thanks,
> -Daniel
> -- 
> -------------

> e: [EMAIL PROTECTED]
> w: http://DanielTse. <http://DanielTse.com/> com/
> -------------
>







-- 
Aaron Miller
Chief Technology Officer
Open Base Interactive, LLC.
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> eractive.com 
http://www.openbase <http://www.openbaseinteractive.com> interactive.com 




-- 
Aaron Miller
Chief Technology Officer
Open Base Interactive, LLC.
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> eractive.com 
http://www.openbase <http://www.openbaseinteractive.com> interactive.com 





-- 
-------------
e: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> com
w: http://DanielTse. <http://DanielTse.com/> com/
------------- 

 

Reply via email to