问题描述
我开始使用 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.
我已经在组件的不同部分成功地使用了这个帮助文件中的不同函数(Joomla:在组件中编写和调用辅助函数).
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:wampwwwilplocallibraries 中的上下文 'JView' 调用受保护的方法 JModel::_createFileName()joomlaapplicationcomponentview.php 在线 773".当我在视图中尝试时,效果很好 - 但我需要模型中的输出.
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:wampwwwilplocallibrariesjoomlaapplicationcomponentview.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:wamppath omodel ot olookups.php 中找不到类 'LookupHelper'.尝试操作 JLoader::register(此处的所有内容)和它不会影响错误消息的路径.
which throws this error: "Fatal error: Class 'LookupHelper' not found in C:wamppath omodel ot olookups.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();
推荐答案
让我们分解一下:
在 Joomla!你的组件助手文件应该在`/mycomponent/helpers/lookup.php'
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'
但您已将组件的路径包含在 dirname
它将仅作为路径的父元素.所以 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:从模型中调用辅助函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!