• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use active records in library

#1
Hello,
i'm writing my personal library for Users.
but i have a problem trying to query my db.
is wrong call db from Lib?

PHP Code:
Class User
{
 
   public function __construct(){
 
      
    
}
 
   
   
    
    public 
function userExists($id){
 
       $query $this->db->get_where('users', array('id' => $id));
 
       $number $query->num_rows();
 
       if($number 0)
 
           return true;
 
       else
 
           return false;
    }
 
       



obviously in my controller i loaded the lib with 

PHP Code:
$this->load->library('user'); 

the error is

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: User::$db

Filename: libraries/User.php

Line Number: 28

Backtrace:

File: /web/htdocs/www.mysite.com/home/API/application/libraries/User.php
Line: 28
Function: _error_handler

File: /web/htdocs/www.mysite.com/home/API/application/controllers/User_authentication.php
Line: 29
Function: userExists

File: /web/htdocs/www.mysite.com/home/API/index.php
Line: 315
Function: require_once


Fatal error: Call to a member function get_where() on a non-object in /web/htdocs/www.mysite.com/home/API/application/libraries/User.php on line 28
A PHP Error was encountered

Severity: Error

Message: Call to a member function get_where() on a non-object

Filename: libraries/User.php

Line Number: 28

Backtrace:

is a good way?
Reply

#2
Libraries are a little different in they don't have immediate access to the `$this` super global.

There's a couple ways to do it but here's how most will tell you to...
PHP Code:
public function __construct()
    {
        
$ci = &get_instance();
    } 


Then, anywhere you use '$this->' use '$ci->' instead,

for example:
PHP Code:
public function userExists($id){
 
       $query $ci->db->get_where('users', array('id' => $id));
 
       $number $query->num_rows();
 
       if($number 0)
 
           return true;
 
       else
            return false
;
 
   
Reply

#3
Here is a template for creating a Custom Library.

PHP Code:
class Custom_Library {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * @var
     * CI Super Object.
     *
     * Example: $this->CI->Library->Method();
     */
    
private $CI;

    
/**
     * @var
     * array ( $index => array ( $key => $val ));
     */
    
protected static $reg = array(array());

    
/**
     * __construct ()
     * --------------------------------------------------------------------
     *
     * Constructor    PHP 5+
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct()
    {
        
$this->CI =& get_instance();

        
log_message('debug'"Custom_Library Class Initialized");
    }


What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
PHP Code:
public function userExists($id){
 
       $query $ci->db->get_where('users', array('id' => $id));
 
       $number $query->num_rows();
 
       if($number 0)
 
           return true;
 
       else
            return false
;
 
   

Thanks guys.. the only correction i've done is row number 2
PHP Code:
$query $ci->db->get_where('users', array('id' => $id)); 
i corrected it as
PHP Code:
$query $this->ci->db->get_where('users', array('id' => $id)); 
Now it works. Rep+ for enlivenapp and InsiteFx
Reply

#5
Also just a note:

It's called Query Builder now not Active Records.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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