CodeIgniter Forums
Can't acccess personnal library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Can't acccess personnal library (/thread-65771.html)



Can't acccess personnal library - Theo - 07-22-2016

Hi all ,

I've just created a library called Cms and i've follow those steps : http://www.codeigniter.com/user_guide/general/creating_libraries.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');
 
$pageName "index";
 
$data['cms'] = $this->Cms->getTags($pageName);
 
$this->load->view('accueil'$data); 

My class is :
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Cms {

 
       public function getTags($pageName)
 
       {
 
        $query $this->db->query("SELECT * FROM cms WHERE 'page_name' = $pageName");

 foreach (
$query->result_array() as $row)
 {
 
       return $row;
 }
 
       }


I've checked many times mispelled word, but everything seems to be ok !!
I'm lost !
Thank for your help !


RE: Can't acccess personnal library - Theo - 07-22-2016

(07-22-2016, 05:48 AM)dubefx Wrote: $data['cms'] = $this->cms->getTags($pageName);
(lower C)

Thank you but it doesn't work !


RE: Can't acccess personnal library - Avenirer - 07-22-2016

You are using CI methods inside the library without instanciating the CI. Take a look here: https://codeigniter.com/user_guide/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library

...I am reffering to the "$this->db..." part.


RE: Can't acccess personnal library - Theo - 07-22-2016

(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/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library

...I am reffering to the "$this->db..." part.

Thank you very much it works !
Here is my new library :
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Cms {

         public function 
__construct()
        {
                
// Assign the CodeIgniter super-object
                
$this->CI =& get_instance();
        }

        public function 
getTags($pageName)
        {
            
            
$query $this->CI->db->get_where('cms', array('page_name' => $pageName), 1);

            foreach (
$query->result_array() as $row)
            {
                    return 
$row;
            }
        }




RE: Can't acccess personnal library - Wouter60 - 07-23-2016

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);
return 
$query->row_array(); 



RE: Can't acccess personnal library - Theo - 07-23-2016

(07-23-2016, 01:39 AM)Wouter60 Wrote: 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);
return 
$query->row_array(); 

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 !


RE: Can't acccess personnal library - PaulD - 07-23-2016

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');

class 
Example_model extends CI_Model {
 
 public function 
__construct()
 {
    
parent::__construct();
 }
 
 public function 
get_user()
 {
    
$this->db->from('whatever');
    
// etc etc.
 
}


Hope that helps

Paul


RE: Can't acccess personnal library - Theo - 07-24-2016

(07-23-2016, 04:33 AM)PaulD Wrote: 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');

class 
Example_model extends CI_Model {
 
 public function 
__construct()
 {
 
   parent::__construct();
 }
 
 public function 
get_user()
 {
 
   $this->db->from('whatever');
 
   // etc etc.
 
}


Hope that helps

Paul

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 !!