AnyCase ModelNames ( Ever wanted to capitalize your models however you wished? ) |
[eluser]Unknown[/eluser]
I'm relatively new to CodeIgniter, but not to programming. I was recently looking through the guide on Models ( http://ellislab.com/codeigniter/user-gui...odels.html ) going through the general topics one by one like a good user, when I found a line that struck me as generally odd, under the heading "Anatomy of a Model." Quote: The underlined portion in particular, is what threw me for a loop. What could possibly be the cause of such a silly rule? Shouldn't I be able to capitalize my Models however I wish? Well I traipsed through the code to find out the reason for this rule. In all technicality, it's an easy fix, but I'm not certain if it's a rule based on the code, or based on a choice. In any case, here's what I found: In ./system/libraries/Loader.php lines 98 through 181 Code: /** This is the model loading function, it's pretty nifty for the most part, but all personal commentary aside, here are the lines in question. lines 153 through 158 Code: $model = strtolower($model); and lines 173 through 175 Code: require_once(APPPATH.'models/'.$path.$model.EXT); What's happening is that we're lowercasing $model with strtolower($model); in order to load the file, and then uppercasing the first letter with ucfirst($model); So, here's one simple way to bypass this behavior. Simply make a $model_file variable, while never touching the original $model's case. :-) Here are the edits: new lines 153 through 160 Code: // BEGIN CUSTOM EDIT <AnyCase ModelNames> and new lines 175 through 180 Code: // BEGIN CUSTOM EDIT <AnyCase ModelNames> And there you have it! You can name your Model something like AwesomeModel now, and it should work, as long as you're doing everything else right! :-) I have yet to look at the loaders for views or controllers, because the user guide entries for them didn't mention any odd naming requirements. Also, I have done a bit of testing with this, but I'm in no way familiar enough with this system to say that this is 100% error proof. I don't know if this is something that is supposed to happen or not, or if it has other effects that I'm not aware of. If you spot something that acts up as a direct result of my code change, let me know and I'll look into it! However, I hope this helps you! Regards, Andrew
[eluser]xwero[/eluser]
I haven't given it much thought because i like the recommended underscore between words rule but for people who follow the camelcase rule it is a limiting factor which isn't necessary. I would change the code slightly different Code: if ( ! file_exists(APPPATH.'models/'.$path.strtolower($model).EXT))
[eluser]Unknown[/eluser]
[quote author="xwero" date="1216314875"]I haven't given it much thought because i like the recommended underscore between words rule but for people who follow the camelcase rule it is a limiting factor which isn't necessary. I would change the code slightly different Code: if ( ! file_exists(APPPATH.'models/'.$path.strtolower($model).EXT)) Aha, it figures I would still be doing something silly even with such a little edit. I like your style better as well :-)
[eluser]Xeoncross[/eluser]
I have a model class called "MYModel" that is in the file "MYModel.php". On my M$ localhost I can load it with Code: $this->load->model('MYModel'); However, this fails on a webserver (linux) stating the the model can't be found. I looked through the code and found that the name of the model is strtolower() so my question is why does it work on my local machine?
[eluser]m4rw3r[/eluser]
Windows is case-insensitive when it comes to filenames, Linux/Unix based systems are case-sensitive. |
Welcome Guest, Not a member yet? Register Sign In |