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.