Welcome Guest, Not a member yet? Register   Sign In
Can't acccess personnal library
#1

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');
 
$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 !
Reply
#2

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

Thank you but it doesn't work !
Reply
#3

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

(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

...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;
            }
        }

Reply
#5

(This post was last modified: 07-23-2016, 01:39 AM by Wouter60.)

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(); 
Reply
#6

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

(This post was last modified: 07-23-2016, 04:34 AM by PaulD.)

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
Reply
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB