• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What are the CI4 ways to use Database and Library in a Helper?

#1
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.
My "Creative Parents" were created me on 11 December of 1990
Reply

#2
(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.
Reply

#3
(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?".
My "Creative Parents" were created me on 11 December of 1990
Reply

#4
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

Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
(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
Reply

#6
(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.
My "Creative Parents" were created me on 11 December of 1990
Reply

#7
(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
My "Creative Parents" were created me on 11 December of 1990
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.