Part of the EllisLab Network
   
 
cannot load model in CI 1.72 using modular extention
Posted: 18 January 2010 05:20 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

hi there, please help my problems to load model in controller inside modules
here’s my code :

class Testing extends Controller {
    
    
function Testing() {
        parent
::Controller();    
    
}
      
    
function getAll() {
       $this
->load->model('Testing2');
       
$data $this->Testing2->getAll();          
    
}    
}


class Testing2 extends Model {
       
    
function Testing2() {
        
// Call the Model constructor
        
parent::Model();
    
}
    
    
function getAll() {
        $items 
= array();
        
$items[0] 'Didit';
    return 
$items;
    
}

my source code locate at system/application/modules/testing/
when i running http://localhost:81/ci-172/index.php/testing/getAll on my IE
i got this messege on the screen

A PHP Error was encountered
Severity
Notice
Message
Undefined propertyCI::$Testing2
Filename
libraries/Controller.php
Line Number
351

Fatal error
Call to a member function getAll() on a non-object in D:\xampp\xampp\htdocs\ci-172\system\application\modules\testing\controllers\testing.php on line 11 

did i miss something ??

thanks

Profile
 
 
Posted: 18 January 2010 05:36 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

do this

$this->load->model('testing2'); 
 Signature 

http://flakron.net

Profile
 
 
Posted: 18 January 2010 05:42 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

waw it’s work, many thanks

Profile
 
 
Posted: 18 January 2010 06:08 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007
flaky - 18 January 2010 10:36 AM

do this

@flaky, This is truly the best help that I have ever seen. wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 18 January 2010 06:18 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

hi all,

i have do some change in the model, see source code below :

class Testing2 extends Model {
       
    
function Testing2() {        
        parent
::Model();
        
$this->load->database();
    
}
    
    
function getAll() {   
        $sql 
"select employeename from employee where isactive = 'Y'";
        
$query $this->db->query($sql);        
        
$items = array();
        foreach (
$query->result_array() as $row{
            $items[] 
$row;
        
}
        $items 
= array();
     return 
$items;
    
}

i got :

A PHP Error was encountered
Severity
Notice
Message
Undefined propertyTesting2::$db
Filename
models/testing2.php
Line Number
13

Fatal error
Call to a member function query() on a non-object in D:\xampp\xampp\htdocs\ci-172\system\application\modules\testing\models\testing2.php on line 13 

please advice , thanks

Profile
 
 
Posted: 18 January 2010 07:04 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

@wiredesignz doing my best

some minor changes

function getAll() {   
        $sql 
"select employeename from employee where isactive = 'Y'";
        
$query $this->db->query($sql)->result_array();        
        
$items = array();
        foreach (
$query as $row{
            $items[] 
$row;
        
}

        
return $items;
    

I recommend you use active record
your method with active record

function getAll() {
        $this
->db->select('employeename');
        
$this->db->where('isactive''Y');
        
$query $this->db->get('employee')->result_array();
        
        
$items = array();
        foreach (
$query as $row{
            $items[] 
$row;
        
}

        
return $items;
    
 Signature 

http://flakron.net

Profile
 
 
Posted: 18 January 2010 09:33 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

hi flaky,

i have changed my source code based on your suggest above, but the system still show the same error

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Testing2::$db
Filename: models/testing2.php
Line Number: 13

error msg (without active record):
Fatal error: Call to a member function query()

error msg (with active record):
Fatal error: Call to a member function select()

why $this->db is not an object???
why Testing2::$db is undefined ??

should i initialize something (db connection, etc) in controller’s constructor ??

Thanks in advance

Profile
 
 
Posted: 18 January 2010 09:35 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

have you configured your database.php?

 Signature 

http://flakron.net

Profile
 
 
Posted: 18 January 2010 09:50 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

yups,

in system/application/config/database.php i just change value of username, passworrd and databasename

$db[‘default’][‘username’] = “root”;
$db[‘default’][‘password’] = “”;
$db[‘default’][‘database’] = “myci”;

just FYI, if i use CI without modular extention everything works fine !!

Profile
 
 
Posted: 18 January 2010 09:51 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

are you autoloading the database?

 Signature 

http://flakron.net

Profile
 
 
Posted: 18 January 2010 09:54 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

no, i don’t

because actually i dont know how to enable / set ‘autoloading’ database,
i dont change anything in the system except for db connection for Mysql above

Profile
 
 
Posted: 18 January 2010 09:56 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

in the same folder where database.php is placed, there is autoload.php
find this part and modify it this.

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your system/application/libraries folder.
|
| Prototype:
|
|    $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'= array('database'); 

then remove

$this->load->database(); 

from the constructor

 Signature 

http://flakron.net

Profile
 
 
Posted: 18 January 2010 10:00 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Avatar
Total Posts:  14
Joined  01-18-2010

wow it’s work!

thanks a lot flaky :D,

Profile
 
 
Posted: 18 January 2010 10:02 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  299
Joined  11-01-2009

no problem
glad I could help grin

 Signature 

http://flakron.net

Profile