Raccoon
Raccoon

Reputation: 1427

CodeIgniter Has relationship between models

I've been googling on this issue for a quite while and still haven't found a satisfactory answer yet :(

It's a small web-based online game where a player will have a game board and a resource board. What is the best way to include those two classes in the player class to create a has-relationship?

Is there a particular way to do it in CI? or just go with include()?

Upvotes: 0

Views: 92

Answers (1)

Philip
Philip

Reputation: 4592

An alternative is dependancy Injection

class userModel extends CI_Model
{
      public function __construct(gameModel $gameModel)
      {
         var_dump($gameModel instanceof gameModel);
      }
}

-

class Controller extends CI_Controller
{
     public function method()
     {
         $this->load->model('gameModel');
         // load model with dependancy on gameModel
         // or vise-verse
         $this->load->model('userModel', new gameModel);
     }
}

-

Or like I mention in comments

class userModel extends CI_Model
{
    public function method()
    {
       $gameModel = $this->load->model('gameModel');
    }

    public function __get($object)
    {
       $instance =&get_instance();
       return $instance->$object;
    }
}

Upvotes: 1

Related Questions