How to properly create a Model instance manually
Posted by Felix Geisendörfer, on Jan 22, 2007 - in PHP & CakePHP » DataSources, Models & Behaviors
Deprecated post
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.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
Hey folks,
sorry for my low activity on here again. The 2nd part of my A PHP developers guide to JavaScript series is in the works but I might need some more days to finish it since I'm still very busy. I've just done 2 little side projects next to my big client project and I'm also working hard on releasing my first own web application (it's still secret - somewhat) so there was little time for blogging.
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:
-
loadModal('MyModel');
-
$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:
-
function &getModel($model)
-
{
-
// Make sure our $modelClass name is camelized
-
$modelClass = Inflector::camelize($model);
-
-
// If the Model class does not exist and we cannot load it
-
{
-
// Can't pass false directly because only variables can be passed via reference
-
$tmp = false;
-
-
// Return false
-
return $tmp;
-
}
-
-
// The $modelKey is the underscored $modelClass name for the ClassRegistry
-
$modelKey = Inflector::underscore($modelClass);
-
-
// If the ClassRegistry holds a reference to our Model
-
if (ClassRegistry::isKeySet($modelKey))
-
{
-
// Then make this our $ModelObj
-
$ModelObj =& ClassRegistry::getObject($modelKey);
-
}
-
else
-
{
-
// 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
-
ClassRegistry::addObject($modelKey, $ModelObj);
-
}
-
-
// Return the reference to our Model object
-
return $ModelObj;
-
}
A sample usage would be this:
-
$User =& $this->getModel('User');
-
-
If ($User!==false)
-
{
-
debug('Yeah!');
-
}
-
else
-
{
-
debug('Oh no!');
-
}
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