Welcome Guest, Not a member yet? Register   Sign In
Model
#1

[eluser]Unknown[/eluser]
Hello

Can someone tell me how to run a Model example, I cant get it working.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$load

Filename: controllers/blog.php

Line Number: 8

Fatal error: Call to a member function database() on a non-object

Code:
<?php

class Blog extends Controller {

    function blog()
    {
    
$this->load->database();

   $this->load->model('blog', '', TRUE);
    
    $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}
?>

Code:
<?php

class Blog extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function Blog()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }
?>

Code:
<html>
<body>
<?php foreach($data as $item):?>
<?php echo $item;?>
<?php endforeach;?>    
</body>
</html>
#2

[eluser]Seppo[/eluser]
You probably want to do something like this, instead

Code:
<?php

class Blog extends Controller {
    // Constructor
    function Blog()
    {
        // always call parent constructor
        parent::Controller();
    }

    function index()
    {
        $this->load->database();

        $this->load->model('blog', '', TRUE);
    
        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}
?>
#3

[eluser]ejangi[/eluser]
I think you'll find the issue is that you essentially have two classes named "Blog". Best bet is to change the name of your model to "Blog_model" and be sure to change the filename to "blog_model.php". Then you just load the model like this instead:
Code:
$this->load->model('blog_model', '', TRUE);
$this->Blog_model->get_last_ten_entries();




Theme © iAndrew 2016 - Forum software by © MyBB