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

[USEFUL! :)] Multiple Models #195

Open
coyarzun2013 opened this issue Sep 7, 2015 · 16 comments
Open

[USEFUL! :)] Multiple Models #195

coyarzun2013 opened this issue Sep 7, 2015 · 16 comments

Comments

@coyarzun2013
Copy link

Hi everyone:

First of all, this is an awesome tiny php script, really great work.

For those who want to use this script with multiple Models, i´ll give you some little changes in the controller.php file

the new constructor function look like this

function __construct()
{
//getting the className of the Controller who extends controller class
$class = get_called_class();
$this->openDatabaseConnection();
//passing this classname as parameter to loadModel Function
$this->loadModel($class);
}

then in loadModel Function

/**
* Loads the "model".
* @return object model
*/
public function loadModel($class=null)
{
(string)$modelFile = strtolower($class);
$instanceClass = $class.'Model';
require APP . 'model/'.$modelFile.'.php';
// create new "model" (and pass the database connection)
$this->model = new $instanceClass($this->db);
}

and finally, declare your model Class name as 'nameModel' for example homeModel, songsModel, etc. and thats it!!!

@panique panique changed the title Multiple Models [USEFUL! :)] Multiple Models Sep 7, 2015
@deathbeam
Copy link

Yey awesome, I vouch for this. 👍

@betweenbrain
Copy link

This is very exciting, I look forward to testing it.

@sanjayrup
Copy link

Hello,

We can have both single, and multiple model.

I have use this exemple for multiple model, and make the older script has $this->utils for shared functions.

I can post a exemple if needed !

@ponsundar
Copy link

This one is what I am looking for.. :) Love you guys..
This peice of work is good.
One more thing, I have customized mini to support sessions and logins.
Shall I Commit the code? Will that be useful?

@skrecker
Copy link

skrecker commented Nov 7, 2015

@coyarzun2013 I have implemented this technique in my project. I actually thought this was implemented in previous versions of the project. I see that this issues has not been closed. I would like to submit changes for this if you are no longer working on the changes.

However, I would make a few changes. The loadModel() method does not need to be so verbose. I would suggest something like this:

 /**
     * Loads the "model".
     * @return object model
     */
    public function loadModel($model_name)
    {

require APP . 'model/' . strtolower($model_name) . '.php';

        return new $model_name($this->db);
    }

As a general suggestion, I would space out your concatenated strings such as:
"$instanceClass = $class.'Model';"
Instead do,
$instanceClass = $class . 'Model';
So that it doesn't look like a method call that you'd see in other languages.

Also, I would suggest keeping the model class and extending it to classes using it. So the model would simply be:

class Model
{
    /**
     * @param object $db A PDO database connection
     */
    function __construct($db)
    {
        try {
            $this->db = $db;
        } catch (PDOException $e) {
            exit('Database connection could not be established.');
        }
    }

This way you can include other methods that you will share across all other models extending the base class.

In order to show how this works l would create a new class, SongsModel, to show the method examples such as getAllSongs, etc.

Let me know what you think.

@ghost
Copy link

ghost commented Dec 14, 2015

great work but why only a single model? yes there previous version had multiple models.
too make it makes an awesome code so limited.
I hope someday to see this update for those of use not using Slim.
cheers.

@panique
Copy link
Owner

panique commented Dec 14, 2015

Hi @mynumbercrunching , because thats the most simple case! when this project had multiple model-classes people were asking to merge everything to one class, now it has one class and people ask to split it to multiple classes. :) it's always a little bit tricky when software is designed by a community.

Personally I think it's okay to stay with one class for now as it's the most simple solution possible and it will work for most users who only need <10 database calls. I you need multiple models, then please have a look at the above solution.

By the way, this script does not use Slim, it's 100% pure native PHP. :)

@sanjayrup
Copy link

ahahahaha ! In my case, i have made a shared model classes, && a multiple classe for each controller ! ;)

@jaonoctus
Copy link

@panique Can you tell me what was the commit where this project was still wearing multiple models?

@skrecker
Copy link

@jaonoctus I'm not sure what commit had the multiple models but it's not that much different. You just need to change the controller class, this way everything else will still be up to date.

In the controller class:

   /**
     * Loads the "model".
     * @return object model
     */
    public function loadModel($model_name)
    {
        require APP . 'model/model.php';
        require APP . 'model/' . strtolower($model_name) . '.php';

        return new $model_name($this->db);
    }

When you create a new model just extend the model class.

@jaonoctus
Copy link

Thank you @skrecker 😄

@jaonoctus
Copy link

@skrecker If i try to load 2 models that extends Model, the application returns the error:

mod_fcgid: stderr: PHP Fatal error:  Cannot redeclare class Model

The right way would be this:

require_once APP . 'model/model.php';

@skrecker
Copy link

@jaonoctus You're right. I've been using only one model in my controllers. Thank you

@jaonoctus
Copy link

@coyarzun2013 @skrecker @mynumbercrunching @sanjayrup @betweenbrain
I Implemented the PSR-4 autoloader to use multiple models and controllers, look:

jaoNoctus/mini

@bestdamnfriend
Copy link

bestdamnfriend commented May 27, 2016

Using this code, I seem to have problems with MINI's default 'Home' model class being loaded, as there is already a controller with the class name 'Home'.

Fatal error: Cannot redeclare class Home

I need to change the loadModel() function to the following:

public function loadModel($model_name = null) {

    require_once APP . 'model/model.php';
    require_once APP . 'model/' . strtolower($model_name) . 'Model.php';

    $model = $model_name . 'Model';
    return new $model($this->db);

}

And then ensure I name my model class 'HomeModel', and the file 'homeModel.php'.

@panique
Copy link
Owner

panique commented May 27, 2016

Good point! This will be fixed with MINI3, where a model is named xxxModel and a controller xxxController! Thanks for the report & code!

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

9 participants