CodeIgniter Forums
What are the CI4 ways to use Database and Library in a Helper? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: What are the CI4 ways to use Database and Library in a Helper? (/thread-74185.html)



What are the CI4 ways to use Database and Library in a Helper? - webdevron - 08-15-2019

I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following:

PHP Code:
$CI =& get_instance();
$CI->load->model('a_modal');
$CI->load->library('a_library'); 

Now I am little bit confused about the CI 4. What are the ways to do this? What is the CodeIgniter way to do it?

Thanks in advance.


RE: What are the CI4 ways to use Database and Library in a Helper? - dave friend - 08-15-2019

(08-15-2019, 10:21 AM)webdevron Wrote: I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following:

PHP Code:
$CI =& get_instance();
$CI->load->model('a_modal');
$CI->load->library('a_library'); 

Now I am little bit confused about the CI 4. What are the ways to do this? What is the CodeIgniter way to do it?

Thanks in advance.

I answered you here, where you previously asked this question.


RE: What are the CI4 ways to use Database and Library in a Helper? - webdevron - 08-15-2019

(08-15-2019, 10:34 AM)dave friend Wrote:
(08-15-2019, 10:21 AM)webdevron Wrote: I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following:

PHP Code:
$CI =& get_instance();
$CI->load->model('a_modal');
$CI->load->library('a_library'); 

Now I am little bit confused about the CI 4. What are the ways to do this? What is the CodeIgniter way to do it?

Thanks in advance.

I answered you here, where you previously asked this question.

Thanks for your response in both threads. It was 15 minutes before my post. Currently I'm solving the issue like bellow.

PHP Code:
// -------------------------- BaseController Constructor
public function initController(......){
 
   $this->_db = \Config\Database::connect();
 
   $this->_model = new \App\Models\a_model();
}
// -------------------------- Function in BaseController
public function some_function( ){
 
   a_helper_function$this ); // Here '$this' is the BaseController Instance


Need your feedback about this solutions.
Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?".


RE: What are the CI4 ways to use Database and Library in a Helper? - kilishan - 08-15-2019

A "Service" is just a function that takes care of initializing an object with any dependencies it needs, and returning it. By default it returns the same instance every time that it's called so it's shared across the application, but you can request a new instance easily.

As for your code example - I wouldn't pass a controller into a helper function. Just pass what it needs, which is likely just the model at that point, since the model contains it's own pointer to the database.

PHP Code:
// -------------------------- Function in BaseController
public function some_function( ){
    
a_helper_function$this->_model ); // Here '$this' is the BaseController Instance




RE: What are the CI4 ways to use Database and Library in a Helper? - dave friend - 08-15-2019

(08-15-2019, 11:41 AM)webdevron Wrote: Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?".

In CIv4 a Service is a type of factory to create new instances of the desired class. In other words, think of it as a kind of "helper" to load and instantiate a class. If you haven't already, read the v4 Documentation - Services info here


RE: What are the CI4 ways to use Database and Library in a Helper? - webdevron - 08-15-2019

(08-15-2019, 12:05 PM)kilishan Wrote: A "Service" is just a function that takes care of initializing an object with any dependencies it needs, and returning it. By default it returns the same instance every time that it's called so it's shared across the application, but you can request a new instance easily.

As for your code example - I wouldn't pass a controller into a helper function. Just pass what it needs, which is likely just the model at that point, since the model contains it's own pointer to the database.

PHP Code:
// -------------------------- Function in BaseController
public function some_function( ){
 
   a_helper_function$this->_model ); // Here '$this' is the BaseController Instance


Thank you kilishan. I just need to know that, My process is not bad and you make me feel that.


RE: What are the CI4 ways to use Database and Library in a Helper? - webdevron - 08-15-2019

(08-15-2019, 12:08 PM)dave friend Wrote:
(08-15-2019, 11:41 AM)webdevron Wrote: Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?".

In CIv4 a Service is a type of factory to create new instances of the desired class. In other words, think of it as a kind of "helper" to load and instantiate a class. If you haven't already, read the v4 Documentation - Services info here

Think of it as a kind of "helper" to load and instantiate a class - this is very much easy to understand. Do not know why, CI4 doc is a little bit harder to understand than CI2 and CI3. But people like you, always make this easy.

Thank you again. Smile