Joomla: calling a helper function from inside a model?

I am starting to develop both php and Joomla, and it is difficult for me to work in Joomla to do fairly simple things. Passed through the example of Joomla MVC and Lynda (and still created some simple views).

I have a helper file / class / function that displays all user identifiers that exist in a "completed" table, so I can display a link for a new record based on this user or modify an existing user record.

I have already used another function in this auxiliary file in another part of the component ( Joomla: Write and calling the auxiliary function in the component ).

When I do the same in the model, I get the following: "Fatal error: invoking the protected JModel :: _ createFileName () method from the" JView "context in C: \ wamp \ www \ ilplocal \ libraries \ joomla \ application \ component \ view.php on line 773 ". When I try to do this in the view, it works fine, but I need the output in the model.

the 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 throws an error: $completions_exist = Jview::loadHelper('lookups');

I found some really vague references to what is called JLoader :: register to enable helper functions, but can't find good documentation about this in Joomla, except for those who say they just use it. I tried using it like this:

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

which causes this error: "Fatal error: class" LookupHelper "was not found in C: \ wamp \ path \ to \ model \ not \ to \ lookups.php. I tried to manipulate JLoader :: register (everything here) and this does not affect error message path.

Thoughts? Why does this work in a view, not a model? How to use helper functions inside the model?

Thank!

#####EDIT

Thanks to @cppl it looks like a problem with the second bit of code. Also I read that .DS. in future versions the notation will be removed, so the code that works:

JLoader::register('LookupHelper', JPATH_COMPONENT_ADMINISTRATOR.'/helpers/lookups.php');
$completions_exist = LookupHelper::completions_exist();
+5
source share
2 answers

Let's break it down:

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

  • JLoader:: - Joomla! , PHP require_once . require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';

  • ? - dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php', dirname, , JLoader /administrator/helpers/lookups.php.

  • JPATH_COMPONENT_ADMINISTRATOR Joomla! renderComponent() JComponentHelper, dirname, , ( ), ./helpers/lookups.php JLoader .

+4

:

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

/events.php .

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

0

All Articles