简体   繁体   English

如何从Joomla中的输入页面获取数据并在控制器中使用它

[英]How to get data from an input page in Joomla and use it in a controller

I have a Joomla xml, defining these two fields among others: 我有一个Joomla xml,其中定义了这两个字段:

<field
    name="country_code"
    type="sql"
    default="10"
    label="COM_TEAM_COUNTRY_CODE"
    query="SELECT country_code, country FROM #__team_country"
    key_field="country_code"
    value_field="country"
    />  
<field name="town" 
    type="text" 
    size="120"  
    class="inputbox span6"
    label="COM_TEAM_FIELD_TOWN_LABEL" 
    description="COM_TEAM_FIELD_TOWN_DESC" 
    required="true" />

These two fields are updated using a function, together with the rest: 这两个字段使用函数进行更新,其余的则进行更新:

<?php
defined('_JEXEC') or die;
class TeamControllerTeam extends JControllerForm
{

    function saveAndSetCity()
    {
            parent::save();
            $jinput = JFactory::getApplication()->input;
            // Get a db connection.
            $db = JFactory::getDbo();


            // Create a new query object.
            $query = $db->getQuery(true);

            // Insert columns.
            $columns = array('town', 'country_code');
            $newCCode  = $jinput->get('country_code','','');
            // Insert values.
            $values = array($db->quote('newTown'), $db->quote($newCCode));

            // Prepare the insert query.
            $query
            ->insert($db->quoteName('#__team_city'))
            ->columns($db->quoteName($columns))
            ->values(implode(',', $values));

            // Set the query using our newly populated query object and execute it.
            $db->setQuery($query);
            $db->execute();
    }
}

The Parent::save line stores it in the table, but I would like to store the specific two fields in another table as well, and the code that I have found and adapted works fine, except for getting the actual value that I want to store, as I haven't found a method that can get the values. Parent :: save行将其存储在表中,但是我也想将特定的两个字段也存储在另一个表中,我找到并改编的代码可以正常工作,除了获得想要的实际值外存储,因为我还没有找到可以获取值的方法。 The code I have made only stores a fixed literal, or blanks no matter how I try. 无论我如何尝试,我编写的代码仅存储固定的文字或空白。 I have read the Joomla documentation page about Jinput, but I did not understood how to use it. 我已经阅读了有关Jinput的Joomla文档页面,但是我不知道如何使用它。

Kind regrads Peter Durup 善待彼得·杜鲁普

I found out that I was using JInput in the wrong way, and I have changed the code so it now works. 我发现我以错误的方式使用了JInput,并且我更改了代码,因此现在可以使用了。

<?php
defined('_JEXEC') or die;
class TeamControllerTeam extends JControllerForm
{

    function saveAndSetCity()
    {
            parent::save();
            $input = JFactory::getApplication()->input;
            $formData = new JInput($input->get('jform','', 'array'));
            // Get a db connection.
            $db = JFactory::getDbo();


            // Create a new query object.
            $query = $db->getQuery(true);

            // Insert columns.
            $columns = array('town', 'country_code');
            //$newCCode  = $jinput->get('country_code','','STR');
            $newTown = $formData->getWord('town');
            $newCCode = $formData->getWord('country_code');
            // Insert values.
            $values = array($db->quote($newTown), $db->quote($newCCode));

            // Prepare the insert query.
            $query
            ->insert($db->quoteName('#__team_city'))
            ->columns($db->quoteName($columns))
            ->values(implode(',', $values));

            // Set the query using our newly populated query object and execute it.
            $db->setQuery($query);
            $db->execute();
    }
}

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

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