Php 如何从Joomla中的输入页获取数据并在控制器中使用
我有一个Joomla xml,其中定义了以下两个字段:Php 如何从Joomla中的输入页获取数据并在控制器中使用,php,joomla3.0,Php,Joomla3.0,我有一个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="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" />
这两个字段与其他字段一起使用函数进行更新:
<?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();
}
}
我发现我用错了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();
}
}
严格来说,您不应该从控制器调用查询。