【问题标题】:Joomla 3.3: Get data from column 'attribs' in the table 'content'Joomla 3.3:从表“内容”中的“属性”列获取数据
【发布时间】:2014-06-07 15:10:03
【问题描述】:

我在我的 Joomla 3.3 网站中使用 Aixeena Easy CCK 插件。这是一个插件,允许我在我的文章编辑页面中添加自定义字段。我在那里填写的内容(应该)显示在我的网站上。该插件将他的信息存储在attribs列的#_content表中。

在他们的网站上,Aixeena 说我必须使用以下代码使填写的文本在我的网站上可见:

$attrb = json_decode($this->item->attribs); 
echo $attrb->fieldname;

此代码丢弃以下错误:

注意:未定义属性:/Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 第 123 行中的 JDocumentHTML::$item

注意:尝试在第 123 行的 /Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 中获取非对象的属性

致命错误:在第 124 行的 /Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 中的非对象上调用成员函数 get()

我认为它是为旧版本的 Joomla 编写的。然后我四处搜索,发现了这段代码:

$params  = $this->item->params;
echo $params->get('fieldname');

当我在我的网站上使用此代码时,它给了我以下错误:

注意:未定义属性:/Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 第 123 行中的 JDocumentHTML::$item

注意:尝试在第 123 行的 /Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 中获取非对象的属性

注意:试图在第 124 行的 /Applications/MAMP/htdocs/buutpot/templates/buutpot.nl-standaardtemplate/index.php 中获取非对象的属性

这没有致命错误。我不知道为什么没有。

谁能帮我找到正确的代码来让我的变量脱离表格?提前致谢!

编辑 1:插件链接:http://www.aixeena.org/aixeena-lab/aixeena-easy-cck

编辑 2:编辑我的问题以回复 Elin 的评论。

【问题讨论】:

  • 当您说“不起作用”时,您需要更加具体。它会抛出错误吗? (你有关于开发的错误报告吗?)它只是回显空白吗?我调试的方法是首先转储 $attrb 并查看 fieldname 是否真的作为属性存在。
  • 你找到解决办法了吗?
  • 很遗憾没有...

标签: plugins joomla


【解决方案1】:

所以就像通知所说的那样,问题是$this->item 不存在。您需要弄清楚对象的实际名称是什么并使用它而不是 $this->item。您可能会通过查看您要显示的任何位置的布局来做到这一点。您能否检查您的模板以查看它是否具有文章视图的布局覆盖(假设您尝试从中访问表单的视图)?

【讨论】:

  • 不,它没有模板覆盖。我使用来自link 的空白模板自己制作了模板。我正在尝试在我的模板的 index.php 文件中显示数据。变量$params 被声明为JFactory::getApplication()->getParams();$thisnot 在我的 index.php 文件中声明的。
  • 不,你永远不会把它放在我们模板的 index.php 文件中!这属于视图的布局文件。你必须考虑事情发生的顺序。模板索引在视图之前处理,所以你还没有可用的 $this->item(通常你不会有 $this->item)。
【解决方案2】:

我使用这段代码对数据库进行了查询:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('attribs');
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id')." = ".JRequest::getInt('id'));

$db->setQuery($query);
$attribs = $db->loadResult();
$attribs = json_decode($attribs, 'true');

$firstattr = $attribs['firstattr'];
$secondattr = $attribs['secondattr'];
$thirdattr = $attribs['thirdattr'];
$fourthattr = $attribs['fourthattr'];

但我相信这可以做得更简单。

【讨论】:

    【解决方案3】:
    $attribs = new JRegistry($article->attribs);
    echo $fieldname = $attribs['fieldname'];
    

    例如,我在 ContentPrepare 上使用此代码,如下所示

    function onContentPrepare($context, &$article, &$params, $page) {
    
            $attribs = new JRegistry($article->attribs);
            //url is my custom field for the content
            $url = $attribs['url'];
            ......
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2018-06-27
      相关资源
      最近更新 更多