Joomla: Call helper function from within a model?

2019-04-08 16:34发布

问题:

I'm starting off with both php and Joomla development, and finding it difficult working within Joomla to do some fairly simple stuff. Went through the Joomla MVC example and Lynda (and have built a few simple views so far).

I have a helper file/class/function that outputs all the userids that exist in the "completed" table so I can display a link for either a new record based on that user or edit an existing user's record.

I've already used a different function in this helper file successfully in a different part of the component ( Joomla: Write and call a helper function in a component ).

When I do the same thing in the model, I'm getting this: "Fatal error: Call to protected method JModel::_createFileName() from context 'JView' in C:\wamp\www\ilplocal\libraries\joomla\application\component\view.php on line 773". When I try it in the view, works fine - but I need the output in the model.

Code:

lookups.php

abstract class LookupHelper {

    public function other_functions($vars){
        ...
    }

    public function completions_exist() {

        $db =& JFactory::getDBO();            
        $query = $db->getQuery(true);

        $query->SELECT(' #__completed.completed_userid as UserID');
        $query->FROM (' #__completed');
        $query->GROUPBY (' #__completed.completed_userid ');

       $db->setQuery($query);    
       $result = $db->loadResultArray(0); 

       return $result;                        

    }        
}

In the model:

$completions_exist = Jview::loadHelper('lookups'); 
$completions_exist = LookupHelper::completions_exist();

This line is throwing the error: $completions_exist = Jview::loadHelper('lookups');

I've found some really vague references to something called JLoader::register to pull in helper functions but can't find any good documentation on that in Joomla except for everyone saying to just use that. SO I tried using it like so:

 JLoader::register('LookupHelper', dirname( JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php');
 $completions_exist = LookupHelper::completions_exist();

which throws this error: "Fatal error: Class 'LookupHelper' not found in C:\wamp\path\to\model\not\to\lookups.php. Tried manipulating the JLoader::register(everything here) and it doesn't effect the path of the error message.

Thoughts? Why does it work in a view and not in the model? How do I use the helper functions within a model?

Thanks!

#####EDIT

Thanks to @cppl looks like it's a path issue with the second bit of code. Also I read that the .DS. notation will be phased out in future versions - so the code that's working is:

JLoader::register('LookupHelper', JPATH_COMPONENT_ADMINISTRATOR.'/helpers/lookups.php');
$completions_exist = LookupHelper::completions_exist();

回答1:

Lets break this down:

  1. In Joomla! your components helper file should be in `/mycomponent/helpers/lookup.php'

  2. JLoader:: is the Joomla! way to do it, but you could just as easily use PHP's require_once eg. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';

  3. Is your path right? - you're providing dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php' but you've wrapped the path to your component in dirname which will the parent element of the path only. So JLoader is looking in /administrator/helpers/lookups.php.

  4. JPATH_COMPONENT_ADMINISTRATOR is initialised as part of Joomla!'s renderComponent() call in it's JComponentHelper class if you apply dirname to it when it's not setup you will get back a dot (ie. current directory) so in the model you could would be passing ./helpers/lookups.php to the JLoader call.



回答2:

You can call helper within model by following method:

JLoader::import('helpers.events', JPATH_COMPONENT);

this will include the file helpers/events.php from the component directory.

$_helper = new EventsHelper(); echo $_helper->getAnyInsideMethod();exit;