Casting Flex objects to Doctrine via Zend_AMF

Casting Flex objects to Doctrine via Zend_AMF

I just had someone ask me about implicitly casting object from Flex to Doctrine via Zend_AMF.   I added this to the Zend issue tracker (http://framework.zend.com/issues/browse/ZF-6843) but no response yet.

Ultimately, this will not be an issue with Doctrine 2.0 but for those of you out there like me that are still using Zend_AMF 1.8 and Doctrine 1.x, here’s some CRUD code that I just pulled from a current project that explicitly casts generic objects I receive from Flex in a Zend_AMF service I have (OrganizationService.php).  You’ll see that I use the Doctrine “merge”, “fromArray” and “assignIdentifier” methods and a php “array” cast to massage what I need.  These techniques are easily adaptable to any Service class and should give you some workarounds.  You’ll need to read http://cesaric.com/?p=303 to understand where the “toAmf” method came from. Take a look at http://cesaric.com/?p=273 also.

class OrganizationService {
 
	public function __construct(){
	}
 
	public function getOrganization(){	
		$q = Doctrine_Query::create()
			->select('o.*, m.*')
			->from('Organization o')
			->leftJoin('o.Individual i')
			->leftJoin('o.modules m')
			->where('i.id = ?',$_SESSION['auth'])
		;
		$orgs = $q->execute();
		$result = $orgs->toAmf(true);
		return $result;
	}
 
	public function putOrganization($object){
		$org = new Organization(); 
		$org->fromArray((array)$object);
		$org->individualId = $_SESSION['auth']; 
		$org->save();
		$result = $org->toAmf(true);
		return $result;
	}
 
	public function setOrganization($object){
		$org = new Organization(); 
		$org->assignIdentifier($object->id);
		$org->merge((array)$object,false);			
		$org->individualId = $_SESSION['auth'];
		$org->save();
		$result = $org->toAmf(true);
		return $result;
	}
 
	public function delOrganization($object){
		$org = new Organization(); 
		$org->assignIdentifier($object->id);
		$result = $org->delete();
		return $result;
	}
 
 
}