Posted on 22/1/07 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
However, over the past days my work has shifted from heavy JS/AJAX/DOM scripting to more PHP related stuff. This means I had some inspirations for PHP-posts again. However, some of them would have been so short that the code would have collided with the rectangular Google-Ad that I used to run. So I finally got tired of it and removed it. I'm thinking of removing the ads on here completely, but I'll wait for my next check from Google before I do so (~$30 left). For the meantime, I've tweaked the ad layout a little. I'm particularly proud about the ad next to the comment box ; ).
Anyway, before this goes totally off-topic, here comes the actual post: When doing more advanced stuff in CakePHP you sometimes need to work with a particular Model outside of a Controller. A good example for this would be a smart helper that creates input fields / widgets based on the field types in the DB like there will be one in Cake 1.2. Now often you'll see people do something like this:
$MyModel =& new MyModel();
Now this one will get the task done - no question about it. But if you aim for better performance or even need some error handling, this won't quite cut it. Therefor I've created a little function that actually will. You pass it the name of any Model, and it will either return you an instance of it or 'false' indicating that it's not possible to get a reference of this Model:
// Make sure our $modelClass name is camelized
$modelClass = Inflector::camelize($model);
// If the Model class does not exist and we cannot load it
if (!class_exists($modelClass) && !loadModel($modelClass))
// Can't pass false directly because only variables can be passed via reference
$tmp = false;
// Return false
// The $modelKey is the underscored $modelClass name for the ClassRegistry
$modelKey = Inflector::underscore($modelClass);
// If the ClassRegistry holds a reference to our Model
// Then make this our $ModelObj
$ModelObj =& ClassRegistry::getObject($modelKey);
// If no reference to our Model was found in trhe ClassRegistry, create our own one
$ModelObj =& new $modelClass();
// And add it to the class registry for the next time
// Return the reference to our Model object
A sample usage would be this:
This of course is not invitation to break up with the MVC pattern, but just a good approach to something you might need for a complex component/helper/etc. If you wonder were to place this function: I like using a component named 'Common' for things like that. This allows you to make static calls like Common::getModel in your project. But AppController, or bootstrap.php should work just as well.
Alright, I hope somebody finds this helpful and that I'll be able to do more postings again ; ).
-- Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.