Welcome Guest, Not a member yet? Register   Sign In
Problem with the Model inclusion
#1

[eluser]Helmasaur[/eluser]
Hi every one,

After a long time, I befan back wirh CI and I have a problem I had already it seems. When I include a model in a controller, it prints it when I load the page and not the view.
Does some one know from where this problem comes from ?

Thanks.
#2

[eluser]BrianDHall[/eluser]
Make sure you are using $this->load->model() and $this->load->view() correctly, or you might indeed be getting some weird results.
#3

[eluser]Helmasaur[/eluser]
This is my controller:

Code:
<?php

class Index extends Controller {

    function __construct()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->model('news', '', TRUE);        
        
        $data['last_news'] = $this->news->get_last_news(5);
        $this->parser->parse('index_view', $data);
    }
}

/* End of file index.php */
/* Location: ./system/application/controllers/index.php */

And this is one part of the model:

Code:
class News extends Model {

    $id;
    $title;
    $category;
    $content;
    $author;
    $ip;
    $timestamp;
    $timestamp_update;
    $viewed;
    
    function __construct()
    {
        parent::Model();
    }

    function get_last_news($number_news)
    {
        $query = $this->db->get('news', $number_news);
        return $query->result();
    }
}
#4

[eluser]jedd[/eluser]
[quote author="Helmasaur" date="1261897850"]
Code:
$this->load->model('news', '', TRUE);
[/quote]

Why don't you choose to follow the user guide's instructions on loading and using models?
#5

[eluser]BrianDHall[/eluser]
Aha, there you go - you call model() with the wrong parameters: http://ellislab.com/codeigniter/user-gui...odels.html

$this->load->model('news'), then you can access $this->news I believe. I'm unsure of how case works off the top of my head, so it might need to be $this->load->model('News', 'news') - can't remember.
#6

[eluser]Eric Cope[/eluser]
are you sure its not your parser lib?
#7

[eluser]Helmasaur[/eluser]
I don't see why you say that I don't respect the guide for including the model… The case doesn't change anything, I tried already.
The parser, it doesn't come from that because when I erase the two lignes under the model inclusion, it still does the same. If I print something with the print function, it's not executed.
#8

[eluser]jedd[/eluser]
[quote author="Helmasaur" date="1261895570"]
When I include a model in a controller, it prints it when I load the page and not the view.
[/quote]

It might be time to describe the nature of your problem with a bit more clarity.

[quote author="Helmasaur" date="1261930806"]
I don't see why you say that I don't respect the guide for including the model… The case doesn't change anything, I tried already.
[/quote]

Ahh, so you do see why I say that you don't respect the guide for including the model - you just don't think that it's important (for reasons that aren't clear).

I would think that if you're having trouble with a model you would try to be extra-careful and follow the user guide to the letter (the case-sensitive letter, in this case).

Quote:The parser, it doesn't come from that because when I erase the two lignes under the model inclusion, it still does the same. If I print something with the print function, it's not executed.

None of the code you have shown includes a 'print something', so it's hard to work out what's failing and how. Perhaps you could show two (or more) code partials that demonstrate different code and describe the different results.

Ideally you can experiment with a simple ->load->model('News') , with subsequent $this->News->etc... calls in there. You know - just in case the manual is accurate.
#9

[eluser]Helmasaur[/eluser]
In the guide, there is no capital letter:
Quote:If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.php you'll load it using:
Code:
$this->load->model('blog/queries');
So, should we put the name of the file or the name of the class I load ?


As I said, this is my controller:
Code:
<?php

class Index extends Controller {

    function __construct()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->model('news', '', TRUE);        
        
        $data['last_news'] = $this->news->get_last_news(5);
        $this->parser->parse('index_view', $data);
    }
}

/* End of file index.php */
/* Location: ./system/application/controllers/index.php */

To see from where it comes from, I modified the function index:
Code:
function index()
{
   $this->load->model('news', '', TRUE);
   echo 'test';    

   // $data['last_news'] = $this->news->get_last_news(5);
   // $this->parser->parse('index_view', $data);
}

My model is:
Code:
class News extends Model {

    $id;
    $title;
    $category;
    $content;
    $author;
    $ip;
    $timestamp;
    $timestamp_update;
    $viewed;
    
    function __construct()
    {
        parent::Model();
    }

    function get_last_news($number_news)
    {
        $query = $this->db->get('news', $number_news);
        return $query->result();
    }
}
When I load the page, there is the model file which is printed.

I hope I've been clear enough.
#10

[eluser]Eric Cope[/eluser]
It sounds like an web server issue. Check the file extensions. Are they any typos?




Theme © iAndrew 2016 - Forum software by © MyBB