Ciao a tutti,
sono alle prese con questo problema: devo recuperare dei dati da una form per salvarli su un DB.
Pubblico il codice per chiarezza:
TABELLE:
CREATE TABLE `#__position` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_open` timestamp NOT NULL default NOW(),
`date_close` timestamp,
`title` varchar(255),
`state_id` int(11) NOT NULL,
`notes` LONGTEXT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
CREATE TABLE `#__position_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL default NOW(),
`id_user` int(11) NOT NULL,
`id_position` int(11) NOT NULL,
`notes` TEXT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
In pratica la position_user mi dirà quali utenti (di sistema) sono associati ad una position.
Vediamo la forms di position.xml
Come potete notare ho aggiunto il campo <field name="iduser" type="hidden" /> che non c'è nella tabella, sulla base di un esempio che ho trovato in rete che fa una cosa simile alla mia (Alexandria Library).
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_lawyer/models/rules"
>
<fieldset>
<field
name="id"
type="hidden"
/>
<field name="iduser" type="hidden" />
<field
name="title"
type="inputbox"
label="COM_LAWYER_POSITION_TITLE_LABEL"
description="COM_LAWYER_POSITION_TITLE_DESC"
size="100"
class="inputbox validate-positions"
validate="title"
required="true"
default=""
/>
<field name="state_id" type="sql"
multiple="false" size="2"
label="COM_LAWYER_POSITION_STATE_LABEL"
description="COM_LAWYER_POSITION_STATE_DESC"
query="select id, description from #__position_state "
key_field="id" value_field="description" required="true">
<option value="">COM_LAWYER_POSITION_SELECT_STATE</option>
</field>
<field name="date_open" type="calendar"
label="COM_LAWYER_POSITION_DATE_OPEN_LABEL"
description="COM_LAWYER_POSITION_DATE_OPEN_DESC"
class="inputbox" size="15"
format="%d-%m-%Y %H:%M" required="true" />
<field name="date_close" type="calendar"
label="COM_LAWYER_POSITION_DATE_CLOSE_LABEL"
description="COM_LAWYER_POSITION_DATE_CLOSE_DESC"
class="inputbox" size="15"
format="%d-%m-%Y %H:%M" required="false" />
<field
name="notes"
type="editor"
buttons="true"
hide="pagebreak,readmore"
filter="safehtml"
label="COM_LAWYER_POSITION_NOTE_LABEL"
description="COM_LAWYER_POSITION_NOTE_DESC"
size="100"
class="inputbox"
validate="title"
required="false"
default=""
/>
</fieldset>
</form>
Qui c'è edit.php del position.
In pratica c'è una lista contenuta in jform_iduser che mi dovrebbe memorizzare i dati degli utenti. Gli utenti vengono caricati dalla tabella di sistema users.
Quindi ho la lista degli utenti associati a position, e la lista degli utenti di sistema. Con i bottoni add e remove sposto gli utenti da una lista all'altra.
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo JRoute::_('index.php?option=com_lawyer&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="position-form" class="form-validate">
<div class="width-60 fltlft">
<fieldset class="adminform">
<legend><?php echo empty($this->item->id) ? JText::_('COM_LAWYER_ADD_NEW_POSITION') : JText::sprintf('COM_LAWYER_EDIT_POSITION', $this->item->id); ?></legend>
<ul class="adminformlist">
<li><?php echo $this->form->getLabel('id'); ?>
<?php echo $this->form->getInput('id'); ?></li>
<li><?php echo $this->form->getLabel('title'); ?>
<?php echo $this->form->getInput('title'); ?></li>
<li><?php echo $this->form->getLabel('date_open'); ?>
<?php echo $this->form->getInput('date_open'); ?></li>
<li><?php echo $this->form->getLabel('date_close'); ?>
<?php echo $this->form->getInput('date_close'); ?></li>
<li><?php echo $this->form->getLabel('state_id'); ?>
<?php echo $this->form->getInput('state_id'); ?></li>
<li>
<label for="iduser"><?php echo JText::_( 'COM_SYSTEM_USERS' ); ?>:</label>
<?php echo JHTML::_('select.genericlist', $this->positionusers, 'jform[iduser][]', 'class="inputbox" size="10" multiple="multiple"','id', 'name', '', 'jform_iduser');?>
<input value="<<?php echo JText::_( 'COM_USER_ADD' ); ?>" onclick="javascript: addSelectedToList( 'adminForm', 'user', 'jform_iduser' );delSelectedFromList( 'adminForm', 'user' );" type="button">
<input value="<?php echo JText::_( 'COM_USER_REMOVE' ); ?> >" onclick="addSelectedToList( 'adminForm', 'jform_iduser', 'user' );delSelectedFromList( 'adminForm', 'jform_iduser' );" type="button">
<?php echo JHTML::_('select.genericlist', $this->users, 'user', 'class="inputbox" size="10" multiple="multiple"','id', 'name'); ?>
</li>
</ul>
<div class="clr"></div>
<?php echo $this->form->getLabel('notes'); ?>
<div class="clr"></div>
<?php echo $this->form->getInput('notes'); ?>
</fieldset>
</div>
<div>
<input type="hidden" name="task" value="position.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
Fin qui tutto funziona alla perfezione.
Il problema è quando salvo ecco il model di position.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* Position Model
*/
class LawyerModelPosition 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 2.5
*/
public function getTable($type = 'Position', $prefix = 'LawyerTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* 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 2.5
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_lawyer.position', 'position', array('control' => 'jform', 'load_data' => $loadData));
// Qui l'XML della form contiene il campo iduser
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the script that have to be included on the form
*
* @return string Script files
*/
public function getScript()
{
return 'administrator/components/com_lawyer/models/forms/lawyer.js';
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 2.5
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_lawyer.edit.position.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
public function save($data)
{
if (parent::save($data)) {
$this->storeuser($data['id'], $data['iduser']);//aggiunto
return true;
}
return false;
}
function storeuser($id, $iduser)
{
$positionusers['id_user'] = 0;
$positionusers['id_position'] = $id;
//e poi si salva....
}
public function getUserslist() {
if (empty( $this->_userslist )) {
$query = ' SELECT * '
. ' FROM #__users'
;
}
if (empty($this->_userslist)) {
$this->_db->setQuery( $query );
$this->_userslist = $this->_getList( $query );
}
return $this->_userslist;
}
public function getPositionsUserslist() {
if (empty( $this->_positionsuserslist )) {
// $bookId = (int) $this->form->getValue('id');
$query = ' SELECT * '
. ' FROM #__position_user JOIN #__users ON #__position_user.id_user = #__users.id'
. ' WHERE id_position = '.(int) $this->getItem()->id;
}
if (empty($this->_positionsuserslist)) {
$this->_db->setQuery( $query );
$this->_positionsuserslist = $this->_getList( $query );
}
return $this->_positionsuserslist;
}
}
nella funzione SAVE mi aspetterei di travare in $data un valore associato a iduser ed invece nulla.
Dov'è che sbaglio?
Cristian