8000 [USEFUL! :)] Multiple Models · Issue #195 · panique/mini · GitHub
[go: up one dir, main page]

Skip to content

[USEFUL! :)] Multiple Models #195

Open

Description

@coyarzun2013

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!!!

Activity

  1. changed the title [-]Multiple Models[/-] [+][USEFUL! :)] Multiple Models[/+] on Sep 7, 2015
  2. deathbeam commented on Sep 8, 2015

    @deathbeam

    Yey awesome, I vouch for this. 👍

  3. betweenbrain commented on Sep 8, 2015

    @betweenbrain

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

  4. sanjayrup commented on Oct 12, 2015

    @sanjayrup

    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 !

  5. ponsundar commented on Oct 13, 2015

    @ponsundar

    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?

  6. skrecker commented on Nov 7, 2015

    @skrecker

    @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.

  7. panique commented on Dec 14, 2015

    @panique
    Owner

    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. :)

  8. sanjayrup commented on Dec 14, 2015

    @sanjayrup

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

  9. jaonoctus commented on Jan 15, 2016

    @jaonoctus

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

  10. skrecker commented on Jan 15, 2016

    @skrecker

    @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.
    
  11. jaonoctus commented on Jan 15, 2016

    @jaonoctus

    Thank you @skrecker 😄

  12. jaonoctus commented on Jan 15, 2016

    @jaonoctus

    @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';
    
  13. skrecker commented on Jan 15, 2016

    @skrecker

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

  14. jaonoctus commented on Jan 22, 2016

    @jaonoctus

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

    jaoNoctus/mini

  15. bestdamnfriend commented on May 27, 2016

    @bestdamnfriend

    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'.

  16. panique commented on May 27, 2016

    @panique
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0