1

I'm having an issue with returning native PHP classes from Flex 4.6 data services.

When I return a PHP class(CustomerDataResponse) with an array of another PHP class(CustomerData), I cannot cast the CustomerData classes in PHP. The Data Servive wizards detect the classes correctly, and testing the service works fine, but the customers array seems to be filled with an array of Objects when I hit it in the debugger, not an array of CustomerData.

If I change the PHP to return stdClass() with the same members and re-run the data wizard, I have to give names to the classes, but the casting works correctly.

Also, the casting of cr.lastResult works fine, only cr.customers.getItemAt(0) seems to be an object and NOT a custoemrdata. I've also confirmed that yes, getITemAt(0) is not null.

Any suggestions? I can use stdClass, but the PHP classes are much nicer to work with.

PHP Classes (trimmed for brevity)

CustomerData.php

class CustomerData {
    public $name;
    public $phoneNumber;

    function __construct(){
        //initialize some values
    }
}

CustomerDataResponse.php:

require_once 'CustomerData.php';
class CustomerDataResponse {

    public $message;
    public $status;
    public $customers;

    function __construct() {
        $this->message = "";
        $this->customers = array(); //Array of CustomerData types
    }
}

CustomerService.php

require_once("Models/CustomerDataResponse.php");

class CustomerInfoService {
    private $customerDataResponse;

    public function __construct() {
        $this->customerDataResponse = new CustomerDataResponse();
    }

    public function getCustomerData(){ //Called from Flex App
        $this->customerDataResponse->customers[] = new CustomerData();
        return $this->customerDataResponse;
    }

FlexApp:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               initialize="initializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.rpc.CallResponder;
            import mx.rpc.events.ResultEvent;

            import services.customerinfoservice.CustomerInfoService;

            import valueObjects.CustomerData;
            import valueObjects.CustomerResponseData;

            private var cr:CallResponder = new CallResponder();
            private var service:CustomerInfoService = new CustomerInfoService()

            protected function initializeHandler(event:FlexEvent):void{
                cr.addEventListener(ResultEvent.RESULT,handleResult);
                cr.token = service.getCustomerData();
            }

            protected function handleResult(event:ResultEvent):void{
                var crd:CustomerResponseData = cr.lastResult as CustomerResponseData; //This works fine
                var cd:CustomerData = crd.customers.getItemAt(0) as CustomerData; //This leaves cd NULL

                trace(cd.name); //Dies here: Cannot access a property or method of a null object reference
            }

        ]]>
    </fx:Script>
</s:Application>
4

0 回答 0