TFennis
TFennis

Reputation: 1423

Best way to manage instance of a model

I'm working on a website and I used CodeIgniter as my framework, but I'm getting increasingly frustrated with the way they abuse OOP. I'm not a fan of OOP but when I use it I prefer a decent implementation. So I've decided to quickly build a PHP framework. Anyways, I'm stuck with the usual MVC architecture for my application but I was wondering what is a good way of passing around models to controllers and libraries. I would prefer to provide an instance of a model to the constructor of a controller. Something along these lines

class MyController implements IController //whatever
{
    public MyController(IUrlRouteModel url_route_model, IUserModel user_model)
    {
        //usual assignment
    }

    public some_action()
    {
         UserEntity users = user_model->find_all();
    }
}

I'm using PHP but this example/question is not directly linked to just PHP, the answer should tho. Now the real question is, where in my could would I manage the instance of the models, where could/should I create them. I will probably call the controller somewhere in the \Router but passing every model to the constructor of the router would be strange. Is there someone who can shed some light on this subject. I've tried to find out how other frameworks do this and this is what i have so far

  1. They use a ModelKeeper that functions as some type of factory for the models, I kind off like this option but the source of that class will get messy
  2. The models are provided in static context. Either every method of the model is static, or they use some type of singleton pattern.

Share your experience stack-overflow? What would be a good way to implement this? Or shorter, what would be an appropriate place to instantiate the models and how?

PS I guess the model would look something like this

class UserModel implements IUserModel
{
     public UserModel(DatabaseConnection dbc)
     { /*store for later use*/ }
}

Upvotes: 2

Views: 307

Answers (1)

tereško
tereško

Reputation: 58444

You should pass an instance of ModelFactory to the controller, which the is used inside of controller(s). The ModelFactory class in the constructor should require a DB connection ( most likely - a PDO instance ). You create instance of it in bootstrap phase ( might be where your \Router is called too ) and , if you need something like that, you can provide the factory with object responsible for caching too.

API would go something like this :

namespace Application;

class FooController
{
    protected $factory = null;

    public function __construct( \Framework\ModelFactory $factory )
    {
        $this->factory = $factory;
    }

    public function bar_action( \Framework\Request $request )
    {
        $model = $this->factory->create( 'User' );
        // then pass this model to your view object 
    }
}

The factory itself looks sort of like this:

namespace Framework;

class ModelFactory
{
    protected $connection = null;    

    public function __construct( \PDO $connection )
    {
       $this->connection = $connection;
    }

    public function create( $name )
    {
        $name = '\\Persistence\\' . $name; 
        // you probably will want configure this in some way

        $model = new $name( $this->connection );
        $model->preconfig(); // .. hell .. maybe you need
        return $model;
    }
}

Your factory can also just receive configuration for DB connection and initialize the PDO object only when it is first time required. It also might be a better choice if you want an ability to juggle connection to multiple databases.

P.S. code was not tested, it was all made up and might contain both syntax and logical fucmess-ups.

Upvotes: 1

Related Questions