Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] let 4-full's controllers be able to handle more than one models #293

Closed
panique opened this issue Nov 22, 2013 · 2 comments
Closed

Comments

@panique
Copy link
Owner

panique commented Nov 22, 2013

Currently the 4-full version uses one model per controller, this means for example a controller Animal uses the animal_model (automatically). I've seen this quite often in several frameworks, but it has the huge disadvantage that only one model can be used at a time.

A more professional architecture would be to let the developer decide which models he/she wants to use in the controller (manually), and

  1. pass all models to the view or
  2. collect data from all models in a $var (let's call it... ehmm.. $model) and pass this $model to the view

I've seen solution #2 in Grails (which is a framework for Groovy), obviously this is also used in other frameworks.

My questions to all of you:

a.) Does anybody know how what the professional name of $model is ? I mean, is THIS the model or are the two used models the models ? Hmm.. it's hard to describe this...

b.) What would be the most professional way to do this ? Is there a definitive way to do this ?

Code of current behaviour:

class Overview extends Controller
{
    function __construct()
    {        
        parent::__construct();
    }

    function showUserProfile($user_id)
    {
        // model is automatically loaded (by the Controller from which we extend)
        // result of the getUserProfile() method is passed to view property
        $this->view->user = $this->model->getUserProfile($user_id);
        $this->view->render('overview/showuserprofile');        
    }    
}

What the new behaviour could look like (partly pseudo-code):

class Overview extends Controller
{
    function __construct()
    {        
        parent::__construct();
    }

    function showUserProfile($user_id)
    {
        // manual loading of model #1
        $overview_model = new Overview_model();
        // manual loading of model #2
        $foo_model = new Foo_model();

        // create empty container for model data
        $model = array();
        // get data from model #1, pass it into an empty variable $model (?)
        $model["user_profile_data"] = $overview_model->getUserProfile($user_id);
        // get data from model #2, pass it into an empty variable $model (?)
        $model["example_foo_data"] = $foo_model->doStuff($user_id);

        // pass the $model to the view (maybe not necessary)
        $this->view->render('overview/showuserprofile', $model);        
    }    
}
@ghost
Copy link

ghost commented Nov 24, 2013

Like I mentioned before, you could accomplish this by autoloading your models. this way you don't have to require your model classes. also I don't see why it is useful to pass the model in the render function. first of all, the name $model (array) is not suitable because it does not contain models but data returned by the models instead. So a more suitable name would be $data= array(). This array contains all data returned by all models. Then just pass the data array to the view the normal way...like so
this->view->data= $data;

#215

@panique
Copy link
Owner Author

panique commented Dec 1, 2013

Done in develop branch! Models are NOT autoloaded, they can be easily loaded within a controller with $x = load_model('mymodel');, $x now contains a new mymodel object. A controller can now load multiple models at once. All models share one database connection btw ;)

@panique panique closed this as completed Dec 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant