0

Since the DataMapper is supposed to be for the exchange/transfer of data between objects and the relational database I would get a user like this

$user = $this->entityFactory->build('User');
$userMapper = $this->mapperFactory->build('User');

$user->setId(43);
$userMapper->fetch($user);

That works fine because I can create the User object outside of the mapper and pass it in but what do I do when I am getting a collection/list of objects?

Creating the empty objects outside of the mapper first just does not seem correct and would surely cause some problems so what is the best way to do it?

Thanks.

ibanore
  • 1,430
  • 1
  • 12
  • 24
  • You mean kinda like described [here](http://stackoverflow.com/a/11943107/727208)? Also, there are two general ways to implement mappers: the simple (naive?) one where you inject the domain object in the mapper, and a structure where you inject a DAO in both mapper and domain object, which then handles the information exchange. – tereško Apr 14 '13 at 06:28

1 Answers1

0

I don't know if this question is still in your mind, however let me give you an answer to this. In principle the first step is the same

$userCollection = $this->entityFactory->build('UserCollection');
$userCollectionMapper = $this->mapperFactory->build('UserCollection');
$user = $this->entityFactory->build('User');
$user->setId(43);
$userCollection->add($user);
$userCollectionMapper->fetch($userCollection);

So the userObject would function here as an searchObject for the collectionMapper (like teresko proposed in an older thread). Your CollectionMapper would retrieve the data from the database and i prefer to use something like

//returns new User object
$newUser = $userCollection->newUser();
//populate user object with DB entry
$userCollectionMapper->populate($newUser,$DBresponse);
//add to collection
$userCollection->add($newUser);

Of course there would be a loop before that looping through the found lines in the database and you would have to clear the list of user objects before adding the results. So this is the way i would deal with the problem. Hope it helps.