Can't acccess personnal library |
Hi all ,
I've just created a library called Cms and i've follow those steps : http://www.codeigniter.com/user_guide/ge...aries.html When i try to access function in my library i obtain error : PHP FATAL ERROR : Call to a member function getTags() on a non-object The controller code is : PHP Code: $this->load->library('Cms'); My class is : PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); I've checked many times mispelled word, but everything seems to be ok !! I'm lost ! Thank for your help !
07-22-2016, 06:07 AM
(This post was last modified: 07-22-2016, 06:14 AM by Avenirer. Edit Reason: mentioning something )
You are using CI methods inside the library without instanciating the CI. Take a look here: https://codeigniter.com/user_guide/gener...ur-library
...I am reffering to the "$this->db..." part. Website: http://avenir.ro
(07-22-2016, 06:07 AM)Avenirer Wrote: You are using CI methods inside the library without instanciating the CI. Take a look here: https://codeigniter.com/user_guide/gener...ur-library Thank you very much it works ! Here is my new library : PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed');
Usually, collecting data from your database is done by a Model, not a Library.
In a model, you can use the $this->db object without instantiating the super-object. One more tip about your method. You are looping through a result_array(), but it seems that you expect the result to be 1 record. In that case you can return this record like this: PHP Code: $query = $this->CI->db->get_where('cms', array('page_name' => $pageName), 1); (07-23-2016, 01:39 AM)Wouter60 Wrote: Usually, collecting data from your database is done by a Model, not a Library. Hello and thank you wouter for your help. I don't really know how to use the personal librairies; but if i understand, i had to call a specific model in the library as we use to do in controllers ? If i use load->model in the library i have to instanciate CI or not ? It's not really clear for me as i develop for the first time my own library ! Thank you very much !
Hi,
In the library you extend CI as you have done. The loader class is in the CI object, so you load the model via your CI instance: PHP Code: $this->CI->load->model('whatever_model'); The model though, as it is loaded into the CI instance, does not need to extend CI, just extends the CI_model as normal. PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); Hope that helps Paul (07-23-2016, 04:33 AM)PaulD Wrote: Hi, Thank you Paul for your help too. I knew i was not using an optimized way to code but it was a first shot . Thank you veryyyy muchhhh !! |
Welcome Guest, Not a member yet? Register Sign In |