Can you access a model from inside another model in CodeIgniter?

后端 未结 4 1497
感动是毒
感动是毒 2021-01-01 14:03

I am writing a webapp using CodeIgniter that requires authentication. I created a model which handles all my authentication. However, I can\'t find a way to access this auth

相关标签:
4条回答
  • 2021-01-01 14:37

    In general, you don't want to create objects inside an object. That's a bad habit, instead, write a clear API and inject a model into your model.

    <?php
    // in your controller
    $model1 = new Model1();
    $model2 = new Model2();
    $model2->setWhatever($model1);
    ?>
    
    0 讨论(0)
  • 2021-01-01 14:44

    Loading a model within a model is now possible with the new CodeIgniter.

    0 讨论(0)
  • 2021-01-01 14:52

    Don't handle authentication in your model. Only use models to interface with your database, or ldap or whatever.

    I created an Auth library that I use to manage authentication and authorization. You can access a library like this from your controllers.

    0 讨论(0)
  • 2021-01-01 14:57

    It seems you can load models inside models, although you probably should solve this another way. See CodeIgniter forums for a discussion.

    class SomeModel extends Model
    {
      function doSomething($foo)
      {
        $CI =& get_instance();
        $CI->load->model('SomeOtherModel','NiceName',true);
    
        // use $CI instead of $this to query the other models
        $CI->NiceName->doSomethingElse();
      }
    }
    

    Also, I don't understand what Till is saying about that you shouldn't create objects inside objects. Of course you should! Sending objects as arguments looks much less clear to me.

    0 讨论(0)
提交回复
热议问题