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

[eluser]JulianM[/eluser]
I'd recommend you to respect the CodeIgniter guidelines and everything will work perfectly.

For example, models should be named as Post_model instead of Post. Controller name should be different than the Model name to avoid conflicts.

Then, in the controller you load the model using:

Code:
$this->load->model('post_model');

And later, you use that model in the controller as shown here:

Code:
$this->post_model->method($param1, $param2);

Good luck.


[quote author="Broxi" date="1246108384"]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[/quote]
#12

[eluser]jedd[/eluser]
[quote author="JulianM" date="1246156898"]I'd recommend you to respect the CodeIgniter guidelines ...

...you use that model in the controller as shown here:

Code:
$this->post_model->method($param1, $param2);
[/quote]

Did you read that link I posted above to the CI User Guide - Models page?

Am I the only one that thought it was confusing to call models by a ucfirst() name, but still did it anyway, whereas everyone else didn't notice, didn't do it, and nothing bad happened to them?

How dreadfully unfair.


Fuzzy - ah, yes, sorry, round the wrong way. This probably exemplifies why an arbitrary 'this class type should be singular, this should be plural' isn't that crash hot an idea. Or at least why it wouldn't work for me.


Broxi - I have a deep and abiding hatred of using plurals in controller, model, table and column names. I can't blame this hatred on a weird uncle, but nonetheless I encourage everyone I encounter to reconsider their usage of plurals in these contexts.
#13

[eluser]Broxi[/eluser]
Hey jedd, thanks again.

I've read through the CI guide models page and re-named the model(s) as per the guidlines i.e modelname_model. I can only blame myself and some other site that I was learning from :cheese:

I've no doubt I will bug ya again soon.
Broxi
#14

[eluser]jedd[/eluser]
I have a comparably passionate hatred of the _model suffix on model names, too .. but on this one I am very much alone I gather.

In my current unlikely-to-ever-be-finished app, users of my site interact with the user stuff by hitting a controller called People, and the backend model that does most of the DB stuff relating to these tasks is called Member. There is not a 'user' in sight - even in my table and column names. (I'm quite happy about this, though I realise this is an uncommon reason for happiness.)

Similarly, my Forum and Mail controllers both talk mostly to my Message model.

I could go on ... and if you stick around these forums you'll see frequent proof of this.
#15

[eluser]Broxi[/eluser]
lmao... your funny ('but in a good way') :-)




Theme © iAndrew 2016 - Forum software by © MyBB