Welcome Guest, Not a member yet? Register   Sign In
load model from own class
#1

[eluser]wiZe[/eluser]
hi there,
i got a problem with loading a model from one of my classes. i created a class:


€dit: ok it is definetly a matter of the class not being able to run the loader class! but it must be really simple, its basicly that:

- create own class in app/libraries
- load an instance of this class in one of my controllers
- load a view/model/whatever from within my own class

Any recommendations?



Code:
<?php
class Sandwhich extends Controller {
    var $id;
    var $Sandwhich;
    
    function Sandwhich ($id)
    {
        $this->build_sandwhich ($id);
    }
    
    function show_sandwhich($id = FALSE)
    {
        $this->load->model('admin/admin_sandwhich _model');
        $poi = $this->admin_sandwhich _model->get_data($id);
    }
}
?>

and instantiated it in my controller:

Code:
<?php
class Controll extends Controller
{
    function show_sandwhich($id = FALSE)
    {
        $this->load->library('Sandwhich', $sandwhich_id);
    }
}
?>

now i recieve this error message:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Admin_sandwhich_model::$db

Filename: admin/admin_sandwhich_model.php

Line Number: 11

Fatal error: Call to a member function select() on a non-object in /path/admin_sandwhich_model.php on line 11

how do i load a model from my own class? hope somebody has an answer for me, thanks in advance!
#2

[eluser]GSV Sleeper Service[/eluser]
looks like the model is being loaded ok, but there's a problem in your model. please post the model code.
#3

[eluser]wiZe[/eluser]
ok this is my loaded model:

Code:
<?php
    class Admin_sandwhich_model extends Model
    {
        function Admin_sandwhich_model()
        {
            parent::Model();
        }
        
        function get_data($id)
        {
            $this->db->select('*');
            $this->db->where('sandwhich_id', $id);
            $this->db->get();
            
            return $query->row_array();
        }
    }
?>

but i think its somethin wrong with the references between the classes. the error "...Call to a member function select() on a non-object..." says that my Sandwhich instance is not an object. or am i wrong?
#4

[eluser]GSV Sleeper Service[/eluser]
the database class is not being loaded in your model.

from the manual
Quote:When a model is loaded it does NOT connect automatically to your database.

for more details see this page - http://ellislab.com/codeigniter/user-gui....html#conn
#5

[eluser]wiZe[/eluser]
sry sleeper i forgot to mention that the db class is being auto loaded. how do i have to extend my own class if i load it in a controller, to get the functionality from controller and model class. i think there is the mistake. thanks for your answer though!!
#6

[eluser]wiZe[/eluser]
ok it is definetly a matter of the class not being able to run the loader class! but it must be really simple, its basicly that:

- create own class in app/libraries
- load an instance of this class in one of my controllers
- load a view/model/whatever from within my own class

Any recommendations?
#7

[eluser]Pascal Kriete[/eluser]
Your library shouldn't extend the controller. Instead, it should be a stand-alone class, that operates on a reference of the current controller. The user guide is a good reference on how this works.

Essentially, you want something like this:
Code:
class Sandwhich {
    var $CI;
    var $id;
    var $Sandwhich;
    
    function Sandwhich ($id)
    {
        $this->CI =& get_instance();
        $this->build_sandwhich ($id);
    }
    
    function show_sandwhich($id = FALSE)
    {
        $this->CI->load->model('admin/admin_sandwhich _model');
        $poi = $this->CI->admin_sandwhich _model->get_data($id);
    }
}
#8

[eluser]wiZe[/eluser]
yea inparo that was the thing i was missing! thanks a lot for your help!
#9

[eluser]meigwilym[/eluser]
BTW, your model get_data method should be

Code:
function get_data($id)
        {
            $this->db->select('*');
            $this->db->where('sandwhich_id', $id);
            $query = $this->db->get(); # > > > > > assign the data to a variable
            
            return $query->row_array();
        }
#10

[eluser]wiZe[/eluser]
i assign it in the controller like:

Code:
$data = $this->db->random_model->get_data($id);




Theme © iAndrew 2016 - Forum software by © MyBB