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

[eluser]kalakazoo[/eluser]
I read on the User Guide under the 'Auto-loading Resources' section that you can autoload 'Models found in the "models" folder' but I'm not sure how.

$autoload['model'] = array('ActivityLog'); Doesn't work but I tried anyways Smile
#2

[eluser]Randy Casburn[/eluser]
@kalakazoo -- OK, I'll ask then :roll:

What does "Doesn’t work but I tried anyways" mean exactly?

What steps did you take to try it?
What were the results that led you to believe this did not work?
What steps did you take to validate this did not work?
What log contents specifically stated "Doesn't work"?
What code did you run when you generated all the data to substantiate all these results?

So on the next post come back with at least SOMETHING from that list of stuff and we'll be able try to figure out why it isn't working.

Randy
#3

[eluser]kalakazoo[/eluser]
jeez it was a simple question, dont blow up. In lamest terms I'm asking "How do you autoload models?"

By doesn't work I mean I don't know what I'm doing and failed...

I figured it had something to do with the autoload file and went with the layout for how the other $autoloads worked. I went to the autoload file and added the line :

$autoload[’model’] = array(’ActivityLog’);

thinking I could maybe make it myself. Really none of this is relevant, I'm just asking how to autoload models. k?
#4

[eluser]hvalente13[/eluser]
kalakazoo,

That should work!

you have to specify the file name of the model without .php extension!

Is 'ActivityLog' right, or if you write it in lowercase 'activitylog' works?

Code:
$autoload[’model’] = array(’activitylog’);

Is it in a subfolder of your models folder 'models/activitylog.php' or 'models/log/activitylog.php'? If second option, you have to specify the subfolder too:

Code:
$autoload[’model’] = array(’log/activitylog’);

Try it and tell if it works!
#5

[eluser]kalakazoo[/eluser]
hvalente13,
'ActivityLog' is right, thats the case of the lettering for the model. I tried fooling with the letter casing in the autoload file but no luck.

It's not in a subfolder of any sort; just models/ActivityLog.php

and no I dont use the .php in the array('ActivityLog') part :/
Not sure what to do but thanks for the advice!
#6

[eluser]Randy Casburn[/eluser]
@kalakazoo -- (It was you #1 post. We get kinda sensitive to folks just say "hey dude - my home work is due tomorrow, can you gimme all the code for that". Clearly you weren't doing that.)

Your issue is kinda tricky to find in the docs...I know you read the docs. That's clear. Its likely the file name that's tripping you up. The file must follow the naming conventions. Change your file name to activitylog.php and change you config file to reflect that name. Things should go well for you.

$autoload['model'] = array('activitylog');

Randy
#7

[eluser]Randy Casburn[/eluser]
And here is the reference from the docs:
http://ellislab.com/codeigniter/user-gui...odels.html

Quote:Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

The file name will be a lower case version of your class name. For example, if your class is this:

The underscore in the model name is not required and not a convention. It can or can not be there but does not have to be there.

Randy
#8

[eluser]kalakazoo[/eluser]
hehe, found a spelling error in the documentation..."If you model is located in a sub-folder," under User Guide -> Models

But yes, I renamed the class ect. and it still is not loading the model.
So now in my autoconfig file I have:
Code:
$autoload['model'] = array('activitylog');
and in my controller i call:
Code:
$this->activitylog->updateActivityLog($this->db->last_query());
and in my model I have:
Code:
<?
class Activitylog extends Model
{
    function Activitylog()
    {
        parent::Model();
    }
    function updateActivityLog($query){..}
#9

[eluser]Randy Casburn[/eluser]
ok...so let me try again...

in the autoload config file the Class name goes in the array()...so that case sensitive name must match the class name... from My config file that works fine...

Code:
$autoload['model'] = array('mTest');

Inside the class, the case sensitive class name must match what is in the config file...this is what is in my test model...that works fine...

Code:
class mTest extends Model {


The file name of that very same model file should be named with all lower case letters...so my test model is named this...and works fine...

Code:
mtest.php


My mTest() class has a method called stuff() so I call it from my controller like this ...it it works fine...

Code:
$this->mTest->stuff();

If you follow that model everything should work fine.

Randy
#10

[eluser]kalakazoo[/eluser]
Your contradicting yourself here with "Class mTest":

Quote:Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase.

I found think link though other forums on codeigniter, my apologizes for not finding it before. Its http://www.derekallard.com/blog/post/aut...deigniter/.

Apparently my CI version is outdated since the last update in which the Loader.php file was changed.

but thanks for the help fellas.




Theme © iAndrew 2016 - Forum software by © MyBB