分享

Joomla MVC

 sumi2005 2011-12-24

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:
  • Digg
  • del.icio.us
  • Twitter
  • StumbleUpon
  • email
  • Facebook
  • Fark
  • LinkedIn
  • Technorati
  • Google Bookmarks
  • Reddit

Tags:

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

  1. 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/

  2. 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. Thanks for the tips. It really helps me a lot doing my project.

  4. 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. 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. Mr Royce,

    Thanks for the note – I have updated the post.

    Kindest regards,

    –Steven Pignataro
    CEO ‘corePHP’

  7. [...] 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 [...]

  8. [...] 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 [...]

  9. Sergejack

    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

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

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

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

  11. webbysignerph

    thanks for this. it really helped me a lot.

  12. 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. Thanks, that really worked well.

  14. Tim

    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?

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多