Hello there!
It is good to reuse functionality whenever you can. Many think that there is a 1 to 1 relationship in regards to model and view, but this is not the case. In fact, the model can be called from anywhere. This means that you can quickly hook into functionality without duplicating code, which is always a good thing!
Let say for example that we have a component called com_foo and we want to access it’s items model from say a module (could be a plugin as well).
//first we import some help from the joomla framework
JLoader::import('joomla.application.component.model');
//Load com_foo's items model (notice we are linking to com_foo's model directory)
//and declare 'items' as the first argument. Note this is case sensitive and used to construct the file path.
JLoader::import( 'items', JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_foo' . DS . 'models' );
//Now instantiate the model object using Joomla's camel case type naming protocol.
$items_model = JModel::getInstance( 'items', 'FooModel' );//Now that we have the item's model loaded, we can set its state
$items_model->setState( 'id', $myItemId );//and use it's methods!
$items_model->get_item();
Tags: Joomla
Nice tutorial, which inspired me to write a followup on HMVC and a Nooku comparison:
http://www./2010/09/14/advancing-from-joomla-mvc-to-nooku-hmvc/
Good tip, much cleaner than doing a require_once!
Also, you can drop the DS constant in favor of /. PHP automatically changes / to \ in paths on Windows. We’re changing it to this for 1.6 core, but it also works now in 1.5.
Thanks for the tips. It really helps me a lot doing my project.
Thanks for the tips. It looks like a typo in
$items_model = JModel::getInstance( Items’, ‘FooModel’ );
An uppercase ‘I’ in items and only one quote.
Just a thing I noticed – there is a naming convention involved in calling a model method.
From the jview::get() method:
#
// Model exists, lets build the method name
#
$method = ‘get’.ucfirst($property);
so if you are sharing a model across multiple views you will want to call like this:
$this->get( ‘method’ );
where the method name in the model is:
function getMethod()
Mr Royce,
Thanks for the note – I have updated the post.
Kindest regards,
–Steven Pignataro
CEO ‘corePHP’
[...] just read a really helpful blogpost over at about how you can reuse a model in Joomla Framework’s MVC structure. Thought I’d give an example of how this is solved in Nooku Framework, and then move on and [...]
[...] just read a really helpful blogpost over at about how you can reuse a model in Joomla Framework’s MVC structure. Thought I’d give an example of how this is solved in Nooku Framework, and then move on and [...]
Why not just doing like this?
JLoader::import(‘joomla.application.component.model’);
// class MyGolfModelMenu defined in a menu.php file
$model = JModel::getInstance(‘menu’, ‘MyGolfModel’);
thank you!!!
i used the shorter version Sergejack suggested
JLoader::import(‘joomla.application.component.model’);
$model = JModel::getInstance(‘menu’, ‘MyGolfModel’);
thanks for this. it really helped me a lot.
To import a another model within a view and have $this->get() work properly you can just add this method to your view and replace banners with the model you want to load.
function get( $method )
{
if( !isset( $this->model ) ) {
JLoader::import( ‘banners’, JPATH_ADMINISTRATOR . DS . ‘components’ . DS . ‘com_banners’ . DS . ‘models’ );
$this->model = JModel::getInstance( ‘Banners’, ‘BannersModel’ );
}
$method = ‘get’ . $method;
return $this->model->{$method}();
}
Thanks, that really worked well.
It works great. Thanks for the tips.
Issues with this approach were the form definition file and language profile could not be found. I have to copy those files to my own component location.
Does anyone have work-around for these?