![]() |
Confusion calling model functions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Confusion calling model functions (/showthread.php?tid=20074) Pages:
1
2
|
Confusion calling model functions - El Forum - 06-27-2009 [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(); Code: Post::getAll(); Firstly is it ok to call models like that and if not, any idea why I get the error. Many thanks Broxi Confusion calling model functions - El Forum - 06-27-2009 [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(); 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) Confusion calling model functions - El Forum - 06-27-2009 [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 And this is mt model 'Post' Code: <?php Still getting the same error, maybe I need some new glasses lol Thanks again Broxi Confusion calling model functions - El Forum - 06-27-2009 [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. Confusion calling model functions - El Forum - 06-27-2009 [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 Confusion calling model functions - El Forum - 06-27-2009 [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... ![]() 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. ![]() Confusion calling model functions - El Forum - 06-27-2009 [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 ![]() [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. Confusion calling model functions - El Forum - 06-27-2009 [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 ![]() [/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. Confusion calling model functions - El Forum - 06-27-2009 [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? Confusion calling model functions - El Forum - 06-27-2009 [eluser]Broxi[/eluser] Thanks for the help guys, I found the problem. It was a typo in My_controller class Later Broxi |