A
A
an232014-10-21 20:48:04
PHP
an23, 2014-10-21 20:48:04

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;
    }

When the entity is more complex, such as a User that also has an avatar, the method looks like this:
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;
    }

1) The question is immediately whether it is correct that UserMapper knows exactly what fields are needed to transfer them to FileMapper. Those. whether this is the correct way to create objects in this case.
2) When I call the getEvents method on the User entity, a collection of objects of type Event is created.
The Event object must contain a reference to the author (User). Thus, when I need to get all events (Events), I make a request like
SELECT * FROM events WHERE id_author=?
i.e. I do not combine Events and Users, which is logical, because I already have the User object I need (it was from him that I called getEvents)
But now there is a problem with the createObject method of the Event object. Here is an example method code:
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;
        }

because i am using Identity Map template, createObject for UserMapper will return me my existing User object.
But there is another situation, when for example I get all Events - then I need a request like
Select * from events join users on events.id_author=users.id

And now the createObject call for userMapper should be like
$userMapper->createObject(array($data['id_author'], $data['firstname'], $data['lastname']));

How to properly distinguish between these 2 conditions in EventsMapper->createObject or do I need to implement it in a different way altogether?
Thanks to

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-10-21
Protko @Fesor

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 question

Ask a Question

731 491 924 answers to any question