1

I'm working on a small test application in PHP that represents the backend of a game. Consider the following:

// GameSession Model
class GameSession {

    private $player;
    ...
}

// Player Model
class Player {

    private $inventory;
    ...
}

// Inventory Model
class Inventory {

    private $extensions;
    ...
}

class InventoryExtension {
    ...
}

class GameController {

    public function newGame() {

        // Create a new game session
        $gameSession = new GameSession();
        $gameSessionMySQL = new GameSessionMySQL();

        ...

        // Save updated game session to DB
        $gameSessionMySQL->update($gameSession);
    }
}

I'm using a Data Mapper Object 'GameSessionMySQL' (class def not shown) to update/save the game session to the database. That mapper knows how to insert, update, retrieve and delete game session data.

What it does not do is update the Player that is inside the GameSession. The Player has its own PlayerMySQL mapper.

In addition, the Player model has an Inventory model, which also has its own InventoryMySQL mapper. The Inventory model has a InventoryExtension. So I have a tree-like structure of objects I guess.

I'm stuck because I don't know where I should be instantiating the appropriate mapper objects and using them? Should I place them all inside the GameController, get()ing the required models from GameSession? That doesn't seem good to me as I might end up writing something like:

$playerMySQL->update($game->Session->getPlayer());
$inventoryMySQL->update($gameSession->getPlayer()->getInventory());
$inventoryExpansionMySQL->update($gameSession->getPlayer()->getInventory()->getExpansion(1);
...

...etc until I end up with a bunch of lines that look suspiciously like procedural.

On the other hand, maybe each model should instantiate its required mapper in its constructor, then call the relevant CRUD methods on it from within its own (probably similarly named) CRUD methods? I doubt this as I'm fairly confident data-access stuff should be kept apart from the domain objects.

I'm implementing all this into a traditional MVC paradigm; that is, where the Controller does nothing at all to the view except accept input from it :P

I'd very much appreciate a clear answer with good reasoning. I'm still learning OO techniques so please go easy on the more arcane terms :)

Thanks very much

whoshotdk
  • 286
  • 2
  • 14
  • Are you sure that you are not confusing [data mapper](http://martinfowler.com/eaaCatalog/dataMapper.html) and [data access object](http://www.codefutures.com/data-access-layer/)? Because those are not the same. – tereško Sep 16 '13 at 00:52
  • I am probably using the wrong terms, yes. I find it much easier to put things into practice than name them! I only just found out about something cool called 'Dependency Injection' only to learn I've already been doing it for years! Mapper or DAO, I don't know - I've got a set of objects - each of which know how to perform CRUD ops on one type of object. Im happy with they way they work. I just dont know where to integrate them into my app. Thanks for the links, I'm checking them now. – whoshotdk Sep 16 '13 at 10:12
  • After further reading I can now identify I have Data Mapper objects. There are some tutorials about these online, but the examples they give just save or update the passed Model's own members and don't explain what to do if a Model contains other Models. I've updated my question with correct terms. Thanks for your help so far :) – whoshotdk Sep 16 '13 at 10:27
  • The other group of object that you have are not "models". They most likely are [domain objects](http://c2.com/cgi/wiki?DomainObject). Maybe [this](http://stackoverflow.com/q/16996516/727208) post would help clearing it up a bit, but IMHO, it looks like you will have to start on reading [**PoEAA**](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420). – tereško Sep 16 '13 at 13:08
  • Thanks for the advice - the book looks like a heavy, but good read! I won't have the time to implement what it discusses in this project however. I've been reading about Service Layers today and have decided to create a GameSessionService whose responsibility it is to interact with the Data Mappers and build/load a complete GameSession domain object (complete with 'children' - player, enemes etc etc) – whoshotdk Sep 16 '13 at 14:10

0 Answers0