Joomla:从模型中调用辅助函数吗? [英] Joomla: Call helper function from within a model?

查看:73
本文介绍了Joomla:从模型中调用辅助函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从php和Joomla开发开始,发现在Joomla中工作很难做一些相当简单的事情.遍历了Joomla MVC示例和Lynda(到目前为止,已经构建了一些简单的视图).

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).

我有一个帮助程序文件/类/函数,它输出已完成"表中存在的所有用户ID,因此我可以基于该用户显示新记录的链接,也可以编辑现有用户的记录.

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 ).

当我在模型中执行相同的操作时,我得到的是:致命错误:在C:\ wamp \ www \ ilplocal \ libraries \中从上下文'JView'调用受保护的方法JModel :: __ createFileName()第773行上的joomla \ application \ component \ view.php".当我在视图中尝试时,可以正常工作-但我需要模型中的输出.

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.

代码:

lookups.php

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;                        

    }        
}

在模型中:

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

此行抛出错误:$completions_exist = Jview::loadHelper('lookups');

我已经找到了一些对JLoader :: register的模糊引用,以引入辅助函数,但是在Joomla中找不到关于它的任何好的文档,除非每个人都说只是使用它.所以我尝试像这样使用它:

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();

这将引发此错误:致命错误:在C:\ wamp \ path \ to \ model \ not \ to \ lookups.php中找不到类'LookupHelper'.尝试操作JLoader :: register(此处所有内容)并它不会影响错误消息的路径.

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?

谢谢!

由于@cppl,这似乎是第二行代码的路径问题.我也读过.DS.符号将在以后的版本中逐步淘汰-因此,有效的代码是:

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. 在Joomla中!您的组件帮助程序文件应位于`/mycomponent/helpers/lookup.php'

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

JLoader::是Joomla!的方式,但是您可以轻松地使用PHP的require_once例如. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';

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';

您的路径正确吗? -您提供的是dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php',但已将组件的路径包装在 将仅是路径的父元素.所以JLoader正在寻找/administrator/helpers/lookups.php.

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.

JPATH_COMPONENT_ADMINISTRATOR作为Joomla!的renderComponent()调用的一部分被初始化为它的JComponentHelper类,如果在未设置的情况下将dirname应用于它,则会返回一个点(即当前目录),因此在模型中您可以将./helpers/lookups.php传递给JLoader调用.

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.

这篇关于Joomla:从模型中调用辅助函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆