[TYPO3-mvc] Mapping various Objects to a Domain Model
Frank Krüger
fkrueger-ml at mp-group.net
Fri Jun 15 08:12:48 CEST 2012
Good morning,
just a raw approach:
- decode your JSON-Data into a php object (json_decode)
- decide which model to use
- map the decoded JSON-Data onto your Model
(dataMapper->map('Tx_MyExt_Domain_Model_ServerXXX',
$myJsonDecodedDataArray);
You can create an instance of the dataMapper within your controller or your
repository using DI by
/**
* @var Tx_Extbase_Persistence_Mapper_DataMapper
*/
protected $dataMapper;
/**
* Injects the DataMapper to map nodes to objects
*
* @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
* @return void
*/
public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper
$dataMapper) {
$this->dataMapper = $dataMapper;
}
Cheers,
Frank
-----Ursprüngliche Nachricht-----
Von: typo3-project-typo3v4mvc-bounces at lists.typo3.org
[mailto:typo3-project-typo3v4mvc-bounces at lists.typo3.org] Im Auftrag von
Thomas "Thasmo" Deinhamer
Gesendet: Freitag, 15. Juni 2012 00:14
An: typo3-project-typo3v4mvc at lists.typo3.org
Betreff: Re: [TYPO3-mvc] Mapping various Objects to a Domain Model
Hello Henjo,
I got several models of type "Server" and those share common properties like
"status" or "os" etc.
class Tx_MyExt_Domain_Model_Server_NewServer
extends Tx_MyExt_Domain_Model_Server;
class Tx_MyExt_Domain_Model_Server_SimpleServer
extends Tx_MyExt_Domain_Model_Server;
class Tx_MyExt_Domain_Model_Server_AwesomeServer
extends Tx_MyExt_Domain_Model_Server;
Now the raw data, which holds the original metadata for each server, is JSON
and every JSON object looks different among the various servers.
NewServer:
{status: 1, os: "Linux"}
SimpleServer:
{running: "yes", operatingSystem: "Windows"}
AwesomeServer:
{offline: FALSE}
// No OS set, but we know "AwesomeServer" runs "Ubuntu".
The Server Model looks like this:
class Tx_MyExt_Domain_Model_Server {
/**
* @var boolean
*/
protected $status;
/**
* @var integer
*/
protected $operatingSystem;
// ...
}
Now I get this raw JSON objects from a web service and I want to have some
kind of helper object or helper functions which map/convert/populate those
JSON objects to my domain models.
I want to encapsulate this conversion/mapping inside a class/several
classes/methods/files.
Actually this is my question. What is the best solution to convert/map those
JSON data to my models in a flexible, extendable, easy to maintain way?
Thanks a lot,
Thomas
Henjo Hoeksma schrieb:
> aren't you just trying to add objects to the server object? If it is
> attached the value is true, if not it isn't?
_______________________________________________
TYPO3-project-typo3v4mvc mailing list
TYPO3-project-typo3v4mvc at lists.typo3.org
http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
More information about the TYPO3-project-typo3v4mvc
mailing list