Welcome Guest, Not a member yet? Register   Sign In
Models not working
#1

[eluser]Kurai[/eluser]
Now I feel really stupid.
I've created a simple database for a very basic blog application. Then I've put some data in the only table (blog_posts) I've got through the scaffolding feature. Then I built up a very basic controller with a "die" statement for testing purposes, and consequently a model which fetches data from the db and then passes them to the controller.

The thing is, controller is working as long as I don't load the model. When I do this, trying to fetch data, all I've got is a blank page. The code is very simple, and I can't understend where the error is. Here's my model:
Code:
<?php
class Blogmodel extends Model{
    
    function Blogmodel{
        parent::Model();
    }
    
    function get_posts(){

        $query = $this->db->get('blog_posts');
        return = $query->result();
    }
    
}

?>

and then the controller:
Code:
<?php
class blog extends Controller{
    
    function Blog(){
        parent::Controller();
        $this->load->model('Blogmodel', 'blog', true);
    }
    function index(){
        
        $data['title'] = "Titolo del blog";
        $data['heading'] = "Primo Blog";
        $data['query'] = $this->blog->get_posts();
        die (print_r($data));
    }
    
}
?>


Probably there's something very stupid I can't get...
#2

[eluser]xwero[/eluser]
try this

[quote author="Kurai" date="1200078812"]
Code:
<?php
class Blogmodel extends Model{
    
    function Blogmodel{
        parent::Model();
    }
    
    function get_posts(){

        $query = $this->db->get('blog_posts');
        return $query->result(); // = removed
    }
    
}
[/quote]
#3

[eluser]Kurai[/eluser]
Not working. If I remove the return statement I get the array, but without query results.
I've noticed a stupid error in my code: function Blogmodel doesn't have brackets. I fixed it, and I still get a blank page.
#4

[eluser]xwero[/eluser]
Have you (auto)loaded your database?
#5

[eluser]eggshape[/eluser]
Hi - I don't think your controller is working the way you want. If you use PHP4-style constructors, the constructor needs to be named exactly like your class. You have 'class blog' but your constructor is 'function Blog'; basically, I don't think your controller is being instantiated.

BTW, PHP5-style constructors are defined as '__construct'. And generally, if you get a completely blank page, there is a PHP syntax error. Oh and I wouldn't beat yourself for these errors; after awhile i'ts hard to see errors in your code. For writers, that's why there are editors and reviewers...Wink
#6

[eluser]tonanbarbarian[/eluser]
there have been some other posts over the last few days mentioning problems with loading models in the constructor on php4

so try the following code (exactly)

Code:
<?php
class Blogmodel extends Model {
    
    function Blogmodel() {
        parent::Model();
    }
    
    function get_posts(){

        $query = $this->db->get('blog_posts');
        return $query->result();
    }

    function get_all_posts() {
        $query = $this->db->query('SELECT * FROM blog_posts');
        return $query->result();
    }
    
}

Code:
<?php
class blog extends Controller{
    
    function Blog(){
        parent::Controller();
    }

    function index(){
        $this->load->model('Blogmodel', 'blog', true);
        $data['title'] = "Titolo del blog";
        $data['heading'] = "Primo Blog";
        $data['query'] = $this->blog->get_posts();
        die (print_r($data));
    }

    function all() {
        $this->load->model('Blogmodel', 'blog', true);
        $data['title'] = "Titolo del blog";
        $data['heading'] = "Primo Blog";
        $data['query'] = $this->blog->get_all_posts();
        die (print_r($data));
    }
    
}

The changes I have made are to change
Code:
return = $query->result();
to
Code:
return $query->result();
as per xwero's suggestion

and to move the load->model into the index method rather than the constructor

finally I have created a second method in the model and controller
so if the blog/index does not work try going to blog/all
If blog/all works and blog/index does not then you have not turned on active records in the database config
Code:
$db['default']['active_r'] = TRUE;




Theme © iAndrew 2016 - Forum software by © MyBB