4

I need to access an administrator end model from a site controller. What's the best way to do this? I have the following in my site controller:

$this->addModelPath(JPATH_COMPONENT_ADMINISTRATOR.'/models');
...
$view = &$this->getView('MyView', 'html');
$view->setModel($this->getModel('MyAdminModel'));
...
$view->display();

then:

$model = $this->getModel('MyAdminModel');
$this->data = $model->get('Items');

in my view, but this seems a bit convoluted. Is there an easier way?

4

Not really, you're doing it right.

You might also need to load the administrator table model (legacy?, are you using J2.5 or 3?). This happens when you need the models outside of your component, not for the views only. In this case, you might want to drop the JPATH_COMPONENT_ADMINISTRATOR constant which would prevent using the models from outside the component. And ensure you are loading the legacy classes with the framework to prevent issues when the legacy classes are moved (they just changed their filesystem location in J 3.3)

Another approach - possibly nicer and easier - would be to inherit the frontend model from the admin model and keep the views simpler. This works because the JModelAdmin extends the JModelForm. Your frontend models would be nearly empty, and you would get a chance to offset there the code that is related to the frontend, keeping the architecture nicer.

  • This is J3.3, but I like the sound of the inheritance approach. Will take a look. Cheers! – codinghands Jul 31 '14 at 8:08
  • @codinghands: Did you solve it by using your first approach or by using inheritance? – Mario Neubauer Jul 29 '15 at 10:04
0

A problem may occur when trying to use the admin version of a class that exists in both: admin and site sides. In this case, doing that is seen as a redeclaration of the class in frontend and not allowed.

An example is ContentModelArticle in Joomla 3.4.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.