Answer the question
In order to leave comments, you need to log in
DataMapper, Identity Map, related objects, how to build the architecture correctly?
Good afternoon.
Implementing the DataMapper template. Got a question. To create an object based on data from the database (an array containing row data), each Mapper object uses the createObject function. Something like:
protected function doCreateObject(array $data) {
$obj = new File();
$obj->setTitle($data['title']);
$obj->setPath($data['path']);
$obj->setType($data['type']);
$obj->setSize($data['size']);
$obj->setId($data['id']);
return $obj;
}
protected function doCreateObject(array $data) {
$obj = new User();
$obj->setFirstName($data['firstname']);
$obj->setLastName($data['lastname']);
$obj->setEmail($data['email']);
$obj->setId($data['id']);
$fileMapper = new FileMapper();
if($data['id_avatar']) {
$fileData = array("id" => $data['id_avatar'],
"path" => $data['path'],
"size" => $data['size'],
"title" => $data["title"],
"type" => $data["type"]);
$avatar = $fileMapper->createObject($fileData);
} else {
$avatar = null;
}
$obj->setAvatar($avatar);
return $obj;
}
SELECT * FROM events WHERE id_author=?
protected function doCreateObject(array $data) {
$obj = new Event();
$obj->setId($data['id']);
$obj->setTitle($data['title']);
$obj->setDescription($data['description']);
$obj->setCreateDate($data['create_date']);
$obj->setTimeStart($data['time_start']);
$obj->setTimeFinish($data['time_finish']);
$userMapper = new UserMapper();
$creator = $userMapper->createObject(array($data['id_author']));
$obj->setCreator($creator);
return $obj;
}
Select * from events join users on events.id_author=users.id
$userMapper->createObject(array($data['id_author'], $data['firstname'], $data['lastname']));
Answer the question
In order to leave comments, you need to log in
I think you should reconsider how you create objects. In particular, you need to store some method data like what to collect and not make a mapper for each object, but simply that, based on the metadata, they would be generated by one mapper that simply knows how to resolve connections.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question