views:

79

answers:

2

I have an iterator service that works fine already and returns a correctly structured values to my flex application through my Zend Amf server

    $contacts = array();

    mysql_connect( 'localhost', 'root', 'test' );
    mysql_select_db( 'test' );

    $res = mysql_query( 'SELECT * FROM contact' );
    while( $contact = mysql_fetch_assoc($res) ) {
          $contacts []= $contact;
    }

   return $contacts;

However I would like to adjust this so that I can leverage my MVC structure and achieve the same results. I have placed an excerpt that can be brought to working condition

 $contacts = array();

 $table = new Model_DbTable_Contact();
 $result = $table->fetchAll();

 //Return an array to be consumed by my flex application     
 foreach ($result as $row)
  {
    /*do something*/
  }

  return $contacts; 
+1  A: 

You'll want to look into ValueObjects. Zend_Amf supports those, and it's a good idea to use that. That way you can have objects that are native to both PHP and Flex.

$server->setClassMap('ContactVO', 'Contact');

Your Flex would then have a class:

[Bindable]
[RemoteClass(alias="Contact")]
public class ContactVO
{
}

Would tell your server that you're going to map your Contact class to ContactVO in Flex.

then you could do:

$data = array();
foreach ($result as $row)
{
    $data[] = new Contact($row); 
    //assuming the Contact constructor parses the array data
}
return $data;

and your Contact objects would get to Flex as ContactVO objects

naneau
I use $server->addDirectory(APPLICATION_PATH . '/services/'); instead so $server->setClassMap('ContactVO', 'Contact'); wount be nesessary
davykiash
A: 

So here I have a function in the logical model for a database table:

public function fetchAll() {

    $resultSet = $this->getDbTable()->fetchAll();
    $entries = array();
    foreach( $resultSet as $row ) {
        $entry = new Model_ClosingData();

        $entry->setId($row->id)
              ->setContractMonth($row->monthId)
              ->setCommodity($row->commodityId)
              ->setDate($row->date)
              ->setPrice($row->price)
              ->setVolume($row->volume)
              ->setOutstandingInterest($row->outstandingInterest);
        $entries[] = $entry;
    }
    return $entries;
}
tebriel