简体   繁体   English

XML中的自定义字段未在数据库中保存参数

[英]Custom field in XML is not saving parametars in DB

This is code which creates custom field: 这是创建自定义字段的代码:

<?php

defined('_JEXEC') or die('Restricted access');

class JFormFieldCustom extends JFormField {

    protected $type = 'Custom';

    protected function getInput() {

        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query = 'SELECT id, title FROM #__content';
        $db->setQuery($query);
        $rows = $db->loadObjectList();

        $result = '';
        foreach ($rows as $row) {
            $result .= '<label>' . $row->title  . '</label><input type=checkbox name=article  value="' . $row->id .'"/>';
        }
        return $result;
    }

}
?>

And the code into XML file: 并将代码转换成XML文件:

<field name="checkboxes" type="custom" default="" label="Articles" description="" />

The problem is that nothing is saved as params into DB. 问题是什么都没有作为参数保存到DB中。 What is wrong here? 怎么了

Several problems. 几个问题。

HTML & PHP Forms HTML和PHP表单

If we unravel your for loop with some dummy data we get the following: 如果我们使用一些伪数据来分解您的for循环,则会得到以下结果:

<label>My Article Title 1</label><input type=checkbox name=article  value="1" />
<label>My Article Title 2</label><input type=checkbox name=article  value="2" />
<label>My Article Title 3</label><input type=checkbox name=article  value="3" />
<label>My Article Title 4</label><input type=checkbox name=article  value="4" />

So the most obvious problem is that you're passing the check boxes back as a single values (all with the same name) where your code indicates that you really want an array. 因此,最明显的问题是您将复选框作为单个值(均具有相同的名称)传递回去,其中代码表示您确实需要数组。

That HTML will render like this: 该HTML将如下所示:

原始复选框

and when submitted PHP will perceive it like this: 在提交时,PHP会这样感知:

var_dump($_POST);

array (size=1)
  'article' => string '3' (length=1)

So, to get an array of a form element in PHP you will need to append [] to the end of the name, ie article[] , to make the display vertical I've also added a <br> after each checkbox and properly quoted the attributes of the <input> . 因此,要在PHP中获取表单元素数组,您需要在名称的末尾附加[] ,即article[] ,以使显示垂直,我还在每个复选框后添加了<br>引用<input>的属性。 That gets us this: 这使我们得到:

<label>My Article Title 1</label><input type="checkbox" name="articles[]"  value="1" /><br>
<label>My Article Title 2</label><input type="checkbox" name="articles[]"  value="2" /><br>
<label>My Article Title 3</label><input type="checkbox" name="articles[]"  value="3" /><br>
<label>My Article Title 4</label><input type="checkbox" name="articles[]"  value="4" /><br>

When this is submitted we can see PHP see's all the check options: 提交后,我们可以看到PHP see的所有检查选项:

var_dump($_POST);

array (size=1)
  'articles' => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '3' (length=1)

Joomla Problems Joomla问题

  1. You create a JDatabaseQuery object with the line $query = $db->getQuery(true); 使用$query = $db->getQuery(true);行创建一个JDatabaseQuery对象$query = $db->getQuery(true); , but then immediately destroy it by overwriting in with a string of SQL. ,然后立即通过用SQL字符串覆盖来销毁它。
  2. Your table and column names are not appropriately quoted, quoting helps to prevent injection risks and reserved word conflicts. 您的表名和列名未正确引用,引号有助于防止注入风险和保留字冲突。 As quoting can vary by the underlying database, JDatabase provides a series of useful functions to make your SQL portable across the various DB's it supports so your query can be rewritten as: 由于基础数据库的报价可能会有所不同,因此JDatabase提供了一系列有用的函数来使您的SQL可跨其支持的各种DB移植,因此您的查询可以重写为:
$query = $db->getQuery(true);
$query->select(array($db->quoteName('id'), $db->quoteName('title')));
$db->setQuery($query);
$rows = $db->loadObjectList();

3. I would recommend not using a generic type of 'Custom' (I realise that lots of tutorial use it but they are just examples), to avoid conflicts try something more descriptive like ArticleCheckboxes . 3.我建议不要使用通用类型的“自定义”(我意识到很多教程都使用了它,但它们只是示例),为避免冲突,请尝试使用更具描述性的内容(例如ArticleCheckboxes So your PHP ends up more like this: 因此,您的PHP最终更像这样:

class JFormFieldArticlesCheckboxes extends JFormField
{

    protected $type = 'ArticlesCheckboxes';

    protected function getInput()
    {

        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query->select(array($db->quoteName('id'), $db->quoteName('title')));
        $db->setQuery($query);
        $rows = $db->loadObjectList();

        $result = '';

        foreach ($rows as $row)
        {
            $result .= '<label>' . $row->title . '</label><input type="checkbox" name="articles[]"  value="' . $row->id . '"/>';
        }

        return $result;
    }
}

4. I presume this is preliminary SQL as right now this will pull back every article in the website, published or not, expired and trashed even. 4.我认为这是初步的SQL,因为现在这将撤回网站上的每篇文章 ,无论是否发表,过期甚至被丢弃。 You may want to consider using the com_content model for articles ContentModelArticles which will give you all the advantages of using a JModelList object, like filtering, and getItems() which returns a list of articles you can use. 您可能要考虑对文章ContentModelArticles使用com_content模型,这将为您提供使用JModelList对象的所有优点,例如过滤,以及getItems() ,该对象返回可以使用的文章列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM