简体   繁体   中英

Joomla 3 - Custom Component - How to access site model from Admin View?

I used component creator to generate a custom component for Joomla 3. I have a view in the Administrator panel that would require a function from a model in the front end.

I have been doing google searches for several days trying to locate an appropriate answer, this is the closest I have come to a working response:

How can I include multiple models in one view for in a Joomla 3.x component built with Component Creator

However, in that response he seems to be using a site view model from another site view.

Here is a little bit about my component structure:

name: com_stargazer

Admin View: email

index.php?option=com_stargazer&view=email&layout=test /administrator/components/com_stargazer/views/email/tmpl/test.php

Site View and model: returnpage

/components/com_stargazer/models/returnpage.php /components/com_stargazer/views/returnpage/tmpl/default.php

I tried to modify my admin view to include the site model by including the path:

    $this->setModel(getModel(JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_stargazer/models', 'returnpageModel')));
    $this->setModel(JModelLegacy::getInstance('returnpage', 'stargazerModel'));
    //  assigns array from the second model to 'ItemsOtherModel.' there is no '$' sign used.
    $this->ItemsOtherModel = $this->get('tags','returnpage');

However, getModel doesn't seem like it's accessible from the view. (Probably the controller only?)

Other, references say to modify the controller (Additional references posted in comments):

https://docs.joomla.org/Using_multiple_models_in_an_MVC_component

Over the last few days, I have tried various iterations of the above referenced code samples . . . Ultimately I am confused about which controller to modify? Do I need to modify the admin controller to get this to work, or the site controller? Would it be easier to add the function to the admin model, and access it on the site view?

It's also been difficult to debug since I don't know which model is throwing the error. My best guess so far though is that I've had NO luck attaching at all to the site model from the admin view. Any help would be appreciated in getting this sorted out.

This is my first question, so I hope that it is clear enough.
I can clarify if needed. Thanks in advance.

To call a frontend or backend Model you can use JLoader or even require_once to include the model file. Using JLoader you can call the model inside admin view like this

JLoader::import('joomla.application.component.model'); //Load the Joomla Application Framework

JLoader::import( 'returnpage', JPATH_SITE  . '/components/com_stargazer/models' ); //Call the frontend model directory


$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model


$tags = $tags_model->gettags();

And you can also use require_once

require_once JPATH_COMPONENT_SITE.'/models/returnpage.php'; 

$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model


$tags = $tags_model->gettags();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM