我使用JTable将数据保存到数据库时遇到问题。我遵循标准的joomla documetation来创建组件。我的数据库表tests
包含id
,title
,description
和created_by
列。我能够使用标题,描述和使用标准保存功能(未编辑)创建表单,将值保存到数据库中。但我想将用户ID插入created_by
列,但不知道如何操作。
不知道我是否可以在edit.php视图tmpl中定义<input type="hidden">
,或者以某种方式编辑控制器或表文件?
无法弄清楚joomla如何使用JTable将表格数据保存到数据库中。
答案 0 :(得分:0)
这是我的test.php tmpl for test view:
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_etesty&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="adminForm">
<div class="form-horizontal">
<fieldset class="adminform">
<legend><?php echo JText::_( 'COM_ETESTY_TEST_DETAILS' ); ?></legend>
<div class="row-fluid">
<div class="span6">
<?php foreach ($this->form->getFieldset() as $field): ?>
<div class="control-group">
<div class="control-label"><?php echo $field->label; ?></div>
<div class="controls"><?php echo $field->input; ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
</fieldset>
</div>
<div>
<?php
$user = JFactory::getUser();
$user_id = $user->id;
echo '<input type="text" name="jform[created_by]" value="' . $user_id . '" />';
?>
<input type="hidden" name="task" value="test.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
这是我的模型test.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* Test Model
*/
class ETestyModelTest extends JModelAdmin
{
/**
* Returns a reference to the a Table object, always creating it.
*
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
* @since 1.6
*/
public function getTable($type = 'Test', $prefix = 'ETestyTable', $config = array())
{
$table = JTable::getInstance($type, $prefix, $config);
return $table;
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
* @return mixed A JForm object on success, false on failure
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_etesty.test', 'test', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_etesty.edit.test.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
这是component / tables / test.php中的表
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');
/**
* Test Table class
*/
class ETestyTableTest extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__etesty_test', 'id', $db);
}
}
我的表单在component / models / forms / test.xml
中<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="nazev"
type="textarea"
label="COM_ETESTY_TEST_NAZEV_LABEL"
description="COM_ETESTY_TEST_NAZEV_DESC"
class="text_area"
default=""
/>
<field
name="popis"
type="text"
label="COM_ETESTY_TEST_POPIS_LABEL"
description="COM_ETESTY_TEST_POPIS_DESC"
size="255"
class="inputbox"
default=""
/>
<field
name="tema"
type="modal_temata"
label="COM_ETESTY_TEMA_LABEL"
description="COM_ETESTY_TEMA_DESC"
/>
</fieldset>
</form>
它用捷克语nazev = title,popis = description。我需要在那里输入用户ID以将其保存到我创建了create_by列的数据库中。
答案 1 :(得分:0)
在您的控制器文件中添加这些代码。不要在表单中添加created_by。
$user = JFactory::getUser();
$data['created_by'] = $user->id;
$model = $this->getModel('yourmodel');
$model->store($data);