Welcome Guest, Not a member yet? Register   Sign In
problem with loading model
#1

[eluser]musikbox[/eluser]
Hi, I'm new to using CodeIgniter so I've probably made some fundamental mistake, please correct!

Anyways I'm trying to load database data with a Model, then pass it through in the controller, however it gives me this error
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Edit_product::$model

Filename: controllers/edit_product.php

Line Number: 21

this is my controller
Code:
public function _construct(){
        parent::_construct();
        $this->load->model('products_model', 'model');  //this seems to be the problem
    }
    
    public function index(){
        $this->load->helper('url');
        $data['base'] = $this->config->item('base_url');
        $data['css'] = $this->config->item('css');
        
        $data['products'] = $this->model->getProducts(); //and this
                
        $this->load->view('edit_product_view',$data);
    }

I also tried calling load without giving the model a name, but that doesn't work either.

Thanks!
#2

[eluser]InsiteFX[/eluser]
Code:
public function _construct()
    {
        parent::_construct();

        $this->load->model('products_model', 'products');  //this seems to be the problem
    }
    
    public function index()
    {
        $this->load->helper('url');
        $data['base'] = $this->config->item('base_url');
        $data['css'] = $this->config->item('css');
        
        $data['products'] = $this->products->getProducts(); //and this
                
        $this->load->view('edit_product_view',$data);
    }

InsiteFX
#3

[eluser]musikbox[/eluser]
that still doesn't work... it seems like php isn't recognizing "products" as a valid object. Are you supposed to also declare products as a variable somewhere?
#4

[eluser]InsiteFX[/eluser]
What version of CodeIgniter our you running

If running CI 2.0 you need do use class Model_name extends CI_Model

Model should be saved with all lowercase characters.

InsiteFX
#5

[eluser]musikbox[/eluser]
I'm using CI 2.0, i think, because my controller classes extend CI_Controller.
I thought that might've worked, but I still get this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Edit_product::$products_model

Filename: controllers/edit_product.php

Line Number: 22

heres the new controller

Code:
<?php

class Edit_product extends CI_Controller {
    
    public function _construct(){
        parent::_construct();
        $this->load->model('products_model');
    }

    public function index(){
        $this->load->helper('url');
        $data['base'] = $this->config->item('base_url');
        $data['css'] = $this->config->item('css');
        
        $data['products'] = $this->products_model->getProducts();
        
        
        $this->load->view('edit_product_view',$data);
    }
    
}

?>

the model is saved as products_model.php
#6

[eluser]bubbafoley[/eluser]
[quote author="musikbox" date="1299910085"]I'm using CI 2.0, i think, because my controller classes extend CI_Controller.
I thought that might've worked, but I still get this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Edit_product::$products_model

Filename: controllers/edit_product.php

Line Number: 22

heres the new controller

Code:
<?php

class Edit_product extends CI_Controller {
    
    public function _construct(){
        parent::_construct();
        $this->load->model('products_model');
    }

    public function index(){
        $this->load->helper('url');
        $data['base'] = $this->config->item('base_url');
        $data['css'] = $this->config->item('css');
        
        $data['products'] = $this->products_model->getProducts();
        
        
        $this->load->view('edit_product_view',$data);
    }
    
}

?>

the model is saved as products_model.php[/quote]

__construct needs 2 underscores. you only have 1.

Code:
public function __construct(){
    parent::__construct();
    $this->load->model('products_model');
}
#7

[eluser]musikbox[/eluser]
Thanks, that fixed it!




Theme © iAndrew 2016 - Forum software by © MyBB