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