Welcome Guest, Not a member yet? Register   Sign In
Confusion calling model functions
#1

[eluser]Broxi[/eluser]
Hi everyone,

I need some help as I am getting a bit confussed. I have a controller class called Posts and a model class names Posts.

If I try to access the model functions with:
Code:
$this->Post->getAll();
I get an Undefined property: Posts::$Post error, but if I use:
Code:
Post::getAll();
It works fine.

Firstly is it ok to call models like that and if not, any idea why I get the error.

Many thanks
Broxi
#2

[eluser]Dam1an[/eluser]
Firstly, you can't have a controller and a model both called Posts, that should have thrown an error
Secondly, when doing
Code:
$this->Post->getAll();
The p in Posts should be lower case, so
Code:
$this->post->getAll();

The reason the second example you show works is cause it's a static funtion, so you use the capitalized name of the class (although it should be Posts, not Post :-S)
#3

[eluser]Broxi[/eluser]
Thanks for the reply, I have tried changing the letters as you suggest but no difference,

This is my controller 'Posts'

Code:
<?php

/**
* by RS Designs
* class Posts
*
*/
class Posts extends My_Controller {

  function Posts(){
    parent::Controller();
    $this->load->model('Post');
  }
    function index() {
      $data['query'] = $this->post->getAll();
      $this->load->view('layout/layout', $data);
    }
}

/* End of file posts.php */
/* Location: ./application/controllers/posts.php */


And this is mt model 'Post'

Code:
<?php

/**
* by RS Designs
* class Post
*/
class Post extends Model {

    function Post(){
        parent::Model();
    }

    function getAll() {
      $this->db->select('categories.id, categories.category, posts.*');
      $this->db->from(array('categories'));
      $this->db->where('posts.category_id = categories.id');
      $data = $this->db->get('posts');
      return $data;
    }
}

/* End of file post.php */
/* Location: ./application/models/post.php */

?>

Still getting the same error, maybe I need some new glasses lol

Thanks again
Broxi
#4

[eluser]jedd[/eluser]
Quote:Still getting the same error, maybe I need some new glasses lol

Hi Broxi - when you say the same error, do you mean the identical, or just similar - sharing a few of the same words?

Perhaps you could cut and paste it.

Aside #1: what do you think this code will do:
Code:
$this->db->from(array('categories'));

Aside #2: I think you should reconsider your model and controller names - Post .v. Posts is really going to confuse you later.
#5

[eluser]Broxi[/eluser]
Thanks for the reply jedd, This is the error I get:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Posts::$Post

Filename: controllers/posts.php

Line Number: 16

Fatal error: Call to a member function getAll() on a non-object in H:\wamp\www\blog\app\controllers\posts.php on line 16


Your point #1 I have already changed, thanks.
#2 Its something I learned early in my php learning that controllers should be plural and modells singular, Is that wrong too?

Later
Broxi
#6

[eluser]TheFuzzy0ne[/eluser]
Just a few observations:

Your model's file name should be lower case, and the model name you pass to the loader should also be lowercase.

If you're extending MY_Controller, you should make a call to parent::MY_Controller(), not parent::Controller().

As Jedd suggested, those names are quite confusing. My solution is to append model names with "_model". So you file would be called post_model.php, the class would be defined as Post_model, and you'd access it using $this->post_model->get(), or whatever. This helps make it clear that you are accessing a model, as opposed to a library.

Lose the camel case. There's just no need for it... Wink

If that method is still non-existent, then there's a good chance you're constructor isn't being called. You can confirm by simply adding a die() within the constructor.

Just my 2 pence. Smile
#7

[eluser]jedd[/eluser]
[quote author="TheFuzzy0ne" date="1246151745"]
Your model's file name should be lower case, and the model name you pass to the loader should also be lowercase.[/quote]

One of us has been doing something wrong for a while, then Wink

[url="http://ellislab.com/codeigniter/user-guide/general/models.html#loading"]CI User Guide - Models[/url]


Didn't even notice that the constructor wasn't calling MY_Controller - yes, that will confuse things.

Agree with the camel case - ugly thing, hard to read, but mostly it's just out of style spec for CI - so you'll have Fun when you start assimilating other people's code.

Models being plural, controllers being singular - an interesting approach. Hadn't heard of that one.

Models should reflect a coherent lump of your data - it may be a table, it may be a few dozen tables. Controllers reflect a grouping of activities. There may be a 1:1 relationship between a controller and a model, but I think people's eagerness to construct that relationship often leads them down (ultimately) confusing design paths. Of course, I may be wrong on this, and may be the confused one. Something to think about, anyway.
#8

[eluser]TheFuzzy0ne[/eluser]
[quote author="jedd" date="1246153227"][quote author="TheFuzzy0ne" date="1246151745"]
Your model's file name should be lower case, and the model name you pass to the loader should also be lowercase.[/quote]

One of us has been doing something wrong for a while, then Wink
[/quote]

Errr - That would be me then... I figured that since I was on a case-sensitive system, that that was the way to do it. Apparently the loader converts it to lower case. Thanks for pointing that out.
#9

[eluser]TheFuzzy0ne[/eluser]
Oh, sorry to double post, but I missed the end of Jedd's last post (Double-D'oh!).

I don't understand. The model is singular, and the controller (if anything) is plural. Is that what you meant?
#10

[eluser]Broxi[/eluser]
Thanks for the help guys, I found the problem. It was a typo in My_controller class

Later
Broxi




Theme © iAndrew 2016 - Forum software by © MyBB