智能早教机器人:Joomla MVC

来源:百度文库 编辑:中财网 时间:2024/05/08 04:46:08

Joomla MVC – Load a model from anywhere!

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();

Share this:

Tags: Joomla

14 Responses to “Joomla MVC – Load a model from anywhere!”

  1. Torkil Johnsen September 14, 2010 at 3:41 pm

    Nice tutorial, which inspired me to write a followup on HMVC and a Nooku comparison:

    http://www.torkiljohnsen.com/2010/09/14/advancing-from-joomla-mvc-to-nooku-hmvc/

  2. Joe LeBlanc September 23, 2010 at 9:42 am

    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.

  3. joomla help October 1, 2010 at 3:16 am

    Thanks for the tips. It really helps me a lot doing my project.

  4. MrRoyce October 8, 2010 at 7:26 am

    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.

  5. Adam Docherty October 8, 2010 at 11:05 am

    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()

  6. Steven Pignataro October 9, 2010 at 3:41 pm

    Mr Royce,

    Thanks for the note – I have updated the post.

    Kindest regards,

    –Steven Pignataro
    CEO ‘corePHP’

  7. Advancing from Joomla MVC to Nooku HMVC | Development Website.in December 1, 2010 at 2:00 am

    [...] just read a really helpful blogpost over at corephp.com 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 [...]

  8. Torkil Johnsen ? Advancing from Joomla MVC to Nooku HMVC December 28, 2010 at 3:17 am

    [...] just read a really helpful blogpost over at corephp.com 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 [...]

  9. Sergejack January 13, 2011 at 5:45 am

    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’);

  10. max4ever April 27, 2011 at 4:23 am

    thank you!!!
    i used the shorter version Sergejack suggested

    JLoader::import(‘joomla.application.component.model’);

    $model = JModel::getInstance(‘menu’, ‘MyGolfModel’);

  11. webbysignerph October 25, 2011 at 4:14 am

    thanks for this. it really helped me a lot.

  12. Adam Docherty October 26, 2011 at 3:33 pm

    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}();
    }

  13. Dirk November 25, 2011 at 2:39 am

    Thanks, that really worked well.

  14. Tim December 14, 2011 at 7:47 pm

    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?