Welcome Guest, Not a member yet? Register   Sign In
load in your library your model
#1

(This post was last modified: 04-12-2015, 10:04 PM by casa.)

hello,
I want to know if you can do this in your own library (directory application/libraries) :
My Library
PHP Code:
class Test
{
    
// need to load model Personne_model before in your controller or in autload config
     
public function get($id)
     {
        
$p = new Personne_model ;
        return 
$p->findPers($id) ;
     }

     
// Test to do this to avoid the precedent method 
     
public function find($id)
     {
          
$this->load->model('personne_model') ;   // here i think i can't do that.. because $this may be define "a not $CI instance"
            // i needed to instance $CI = & get_instance()  and after $CI->load->model('personne_model') ;
          
return $this->personne_model->findPers($id) ;  // this is not good and not works
          
           //but if i do that i's ok.. but i think it's perhaps a bad idea to that too.
     /*     $CI = & get_instance()
            $CI->load->model('personne_model') ; 
             return $CI->personne_model->findPers($id) ;  // ok 
     */
     
}
      
// i have an error

When i load my library in my controller and then i want to use my function find($id) of the library, i have an error :
- Message : undefined property $load in Test
- Fatal error: Call to a member function model() on a non-object

How do you do to use your model in your library without instanciate an object of your model and not load your model before, tha is to say, to avoid that :
My Controller
PHP Code:
class Controleur extends CI_Controller
{
      public function 
__constrcut() { parent::__construct ;}
     
      
// this is ok
      
public function index()
      {
            
// loading model
           
$this->load->model('Personne_model') ;
            
// loading library who uses this model
           
$this->load->library('Test') ;
            
// calling library method who use my model 'personne_model' 
           
$object $this->Test->get(2) ;  // ok

            
$data = array('obj' => $object) ;
            
$this->load->view('....'$data) ;
      }
 
     
// this is not ok
      
public function index_new()
      {
           
           
$this->load->library('Test') ;
           
$object $this->Test->find(2) ;  // fatal error

            
$data = array('obj' => $object) ;
            
$this->load->view('....'$data) ;
      }

If there is an issue thanks otherwise, i do like my function index() of my controller. Perhaps, it's a bad idea to try to do like the method find($id) in library Test.
Reply
#2

Your comments in the code imply that you are on the right track, but for some reason are reluctant to do it the way it has to be done.

PHP Code:
<?php

class Test
{
 
   private $ci;

 
   public function __construct()
 
   {
 
       $this->ci =& get_instance();
 
       $this->ci->load->model('personne_model');
 
   }

 
   public function get($id)
 
   {
 
       return $this->ci->personne_model->findPers($id);
 
   }

 
   public function find($id)
 
   {
 
       return $this->ci->personne_model->findPers($id);
 
   }


Of course, since both methods do the same thing, you could just call $this->get($id); from the find() method. You could also assign another variable in the library to the model to avoid the chaining through the ci property:

PHP Code:
$this->personne_model $this->ci->personne_model

Note that $this->load->model() does not return an instance of the model, so you have to reference the CI instance at least twice in your library in order to load your model and use the model.

You can't use new to instantiate your model directly (as in your original get() method), unless your model does not extend CI_Model and is built with this use in mind.
Reply
#3

(This post was last modified: 04-15-2015, 02:07 AM by casa.)

thanks for you advise.
I just have an another question about the fact that i can't use "new" to instanciate my model named "Personne_model" :
My model "Personne_model" extends CI_MODEL.
I'm not very good in english and i just want to know if your method :
PHP Code:
<?php

class Test
{
    private 
$ci;
    
    public function 
__construct()
    {
        
$this->ci =& get_instance();
        
$this->ci->load->model('personne_model');
    }

    public function 
get($id)
    {
        return 
$this->ci->personne_model->findPers($id); // don't need to load the model in my controller before load my library and using it
    
}

?>
is the best practice ? or if
PHP Code:
<?php

class Test
{
    private 
$ci;

    public function 
__construct()
    {
        
$this->ci =& get_instance();
    }

    public function 
get($id)
    {
        
$p = new Personne_model ;  // and in my controller i load the model before load this library
        
return $p->findPers($id);
    }

   

is a good practice too ?
Thanks.
Reply
#4

If you are loading anything which extends something in CI, you need to use CI's loader instead of instantiating it with new. So, if your model extends CI_Model, you need to use $this->load->model() to load your model. It's not even a question of good practices at this point, as using new just won't work without a lot of other supporting code.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB