Best way to extend a custom model

edited April 2013 in Modules
Using v1.0.

I created a model called client_games_model connected to the client_games table. Of course, there are prizes_model, players_model, game_types_model, etc. There's also a games_model that's sort of a master model for each game type. Basically I'll have multiple instances of each game type associated with various clients.

I started with just one game type (scratch-off game) to use for testing while getting all other aspects of this site set up and I know that I should have thought this through better, but now that I'm ready to add another game type I've got a chocolate mess.

I have 3 fairly large methods inside the client_games_model that are specific to the scratch-off-game. All other methods in the client_games_model will apply to all games.

I'd like to know from any of you smarter, more well-versed folks the best way to extend the client_games_model so that I have a separate set of methods for each type of game that would be unique to each game. My thought was that if I had a model like scratch_games_model that extended client_games_model I could just house the scratch-off game specific methods in there but make them accessible to views through the client_games_model. I know I'm not seeing the whole picture and I could probably just keep adding methods to the client_games_model but it's gonna get a little out of hand. I read some about My_model but I'm not quite getting how I could use that here. Also wondered about helper functions, etc. It's really only going to be a handful of game-type-specific functions that need to be directly associated with client_games_model.

Here's the client_games_model.php code through construct. No sense in pasting the whole thing.
class Client_games_model extends Base_module_model { public $required = array('new_game_name','permalink'); public $foreign_keys = array('game_id' => 'games_model', 'client_id' => 'clients_model', 'theme' => 'image_themes_model', 'custom_theme' => 'agency_image_themes_model'); public $has_and_belongs_to_many = array('prize_id' => 'prizes_to_client_games_model'); public $has_many = array('power_prizes' => array('app' => 'power_prizes_model')); function __construct() { parent::__construct('client_games'); //table }
Any thoughts? Thanks in advance for the help, btw.

Comments

  • edited 5:18PM
    It sounds like your thought of having a base class that you inherit from for basic functionality and then customize for model specific methods would be a good approach. There are also interfaces which a class can implement.
    http://php.net/manual/en/language.oop5.interfaces.php
    http://php.net/manual/en/function.class-implements.php
  • edited April 2013
    You were right. This turned out to be very simple! All I had to do was this:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Scratch_games_model extends Client_games_model { // functions here }
    All I had to do was snag the functions that were specific to scratch games and put them in the above model extending the client games model. Of course, in my scratch game controller, I just had to load scratch_games_model after the client_games_model. It works almost like a helper file except it has the advantage of being able to inherit from the client_games class.
    Perfecto!
Sign In or Register to comment.