Welcome Guest, Not a member yet? Register   Sign In
cannot load model in CI 1.72 using modular extention
#1

[eluser]didit[/eluser]
hi there, please help my problems to load model in controller inside modules
here's my code :

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

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$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
#2

[eluser]flaky[/eluser]
do this
Code:
$this->load->model('testing2');
#3

[eluser]didit[/eluser]
waw it's work, many thanks
#4

[eluser]wiredesignz[/eluser]
[quote author="flaky" date="1263828987"]do this[/quote]

@flaky, This is truly the best help that I have ever seen. Wink
#5

[eluser]didit[/eluser]
hi all,

i have do some change in the model, see source code below :
Code:
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 :

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Testing2::$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
#6

[eluser]flaky[/eluser]
@wiredesignz doing my best

some minor changes
Code:
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
Code:
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;
    }
#7

[eluser]didit[/eluser]
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
#8

[eluser]flaky[/eluser]
have you configured your database.php?
#9

[eluser]didit[/eluser]
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 !!
#10

[eluser]flaky[/eluser]
are you autoloading the database?




Theme © iAndrew 2016 - Forum software by © MyBB