CodeIgniter Forums
Calling library method inside model error (with solution) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Calling library method inside model error (with solution) (/showthread.php?tid=71810)



Calling library method inside model error (with solution) - elche - 09-26-2018

Hi everyone,
I was spent a lot of time solving this situation in a my current project and I would like to share with all of you this solution to whom may help. 
It's a matter of calling a class method from model method called from a constructor without class being completely loaded. Probably Too trivial.. and for beginners but.. we do not always have a good day  Rolleyes

Suppose we have this case: 
(I apologize about wrong code, capitals, etc.. I have rewritten this code without testing it, but making it similar and removing useless information from my current project)


Controller mycontroller
-------------------------


Code:
class mycontroller extends CI_Controller() {
 function __construct() {
   parent::__construct();
   $this->load->library("folder/mylibrary");
 }
}


library mylibrary
-----------------

Code:
protected $obj;
class mylibrary() {
 function __construct() {
   $this->obj =& get_instance();
   $this->obj->load->model("folder/mymodel");
   $this->obj->load->library("folder/mysecondlibrary");
 }
}



library mysecondelibrary
-------------------------


Code:
class Mysecondlibrary{

 protected $obj;

function __construct() {
$this->obj = & get_instance();
$this->obj->mymodel->mymodelmethod();
}

function mymethod() {
return true;
}
}


model mymodel
-----------------

Code:
class Mymodel extends CI_Model {
        
    function __construct() {
        parent::__construct();
    }

   function mymodelmethod() {
       $this->mysecondlibrary->mymethod(); /* This crash: Undefined property: Mycontroller :: mylibrary Filepath: system\core\Model.php, [..]
   }
}



If we call a library method from model method which it was called from a library constructor class this will crash. We need to call method after
libraries are loaded.

To solve this, we need to call a class init method after loading.

library mylibrary
-----------------

Code:
protected $obj;
class mylibrary() {
 function __construct() {
    $this->obj =& get_instance();
    $this->obj->load->model("folder/mymodel");
    $this->obj->load->library("folder/mysecondlibrary");

    /** ADDED */
    $this->mysecondelibrary->init();
 }
}



library mysecondelibrary
-------------------------


Code:
class Mysecondlibrary{

  protected $obj;

  function __construct() {
    $this->obj = & get_instance();
   /** Mymodel Method call removed */
  }

  /** ADDED */
  function init() {
    $this->obj->mymodel->mymodelmethod();
   }

  function mymethod() {
    return true;
  }
}



RE: Calling library method inside model error (with solution) - Wouter60 - 09-26-2018

Whats the reason why you are loading the second library into the first one? Wouldn't it be better to:
PHP Code:
class Mysecondlibrary extends Mylibrary 

That way, you can call methods in Mylibrary through $this->mysecondlibrary->


RE: Calling library method inside model error (with solution) - elche - 09-26-2018

(09-26-2018, 01:15 PM)Wouter60 Wrote: Whats the reason why you are loading the second library into the first one? Wouldn't it be better to:
PHP Code:
class Mysecondlibrary extends Mylibrary 

That way, you can call methods in Mylibrary through $this->mysecondlibrary->

Thanks for your contribution, in this case I am using mylibrary only to load all libraries, methods, helpers into the memory but mylibrary has no methods or variables that I can use from mysecondlibrary.


RE: Calling library method inside model error (with solution) - Gurutechnolabs - 10-01-2018

Hello,

you can call methods in My library through $this->mysecondlibrary->
class Mysecondlibrary extends My library

<?php
class Blog extends CI_Controller {

public function index()
{
echo 'Hello World!';
}
}