Hello,
I'm having troubles doing a dinamyc list with a combobox on each row allowing users to select an option and saving them all with a single save button.
I'll post as i have it right now, probably bad after a lot of tries and editions. It's still throwing this error:
Datawriter
Template
Hope someone could help here
PS: All query are requesting all fields (select *) so no info missed there.
Regards
I'm having troubles doing a dinamyc list with a combobox on each row allowing users to select an option and saving them all with a single save button.
I'll post as i have it right now, probably bad after a lot of tries and editions. It's still throwing this error:
- The existing data required by the data writer could not be found.
- The field 'training_group' was not recognised.
Code:
public function actionTraining ()
{
//Selecciona la ID del usuario
$_userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
if ($_userId){
$userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
}else{
$userId = XenForo_Visitor::getUserId();
}
$teamId = ($this->getModelFromCache('XenForo_Model_Teams')->getTeamId($userId));
$team = ($this->getModelFromCache('XenForo_Model_Teams')->getTeamByUser($userId));
$players = ($this->getModelFromCache('XenForo_Model_Teams')->getPlayers($teamId));
$viewParams = array(
'players' => $players,
'team' => $team
);
return $this->responseView('XenForo_ViewPublic_Misc_Empty', 'team_training', $viewParams);
}
public function actionTrainingPlayerUpdate ()
{
$visitor = XenForo_Visitor::getInstance();
if (!$visitor->get('user_id')) {
return $this->responseError(new XenForo_Phrase('cannot_edit_training'));
}
$userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
$settings = $this->_input->filter(array(
'player_id' => array(XenForo_Input::UINT, 'array' => true),
'training_group' => XenForo_Input::STRING)
);
$writer = $this->_savePlayer($settings, $errors);
return $this->responseRedirect(
XenForo_ControllerResponse_Redirect::SUCCESS,
XenForo_Link::buildPublicLink('team/training'),
null);
}
protected function _savePlayer($settings, &$errors)
{
$writer = XenForo_DataWriter::create('XenForo_DataWriter_Player');
$writer->setExistingData($settings['player_id']);
$writer->set('training_group', $settings['training_group']);
$writer->preSave();
/*if ($dwErrors = $writer->getErrors())
{
$errors = (is_array($errors) ? $dwErrors + $errors : $dwErrors);
return false;
}*/
$writer->save();
return $writer;
}
Datawriter
Code:
class XenForo_DataWriter_Player extends XenForo_DataWriter
{
protected function _getFields()
{
array(
'xf_player' => array(
'player_id' => array( 'type' => self::TYPE_UINT, 'autoIncrement' => true ),
'training_group' => array('type' => self::TYPE_UINT, 'required' => false, 'default' => ''),
)
);
}
/**
* Gets the actual existing data out of data that was passed in. See parent for explanation.
*
* @param mixed
*
* @see XenForo_DataWriter::_getExistingData()
*
* @return array|false
*/
protected function _getExistingData($data)
{
if (!$teamId = $this->_getExistingPrimaryKey($data, 'player_id'))
{
return false;
}
return array('xf_player' => $this->_getPlayerModel()->getPlayer($playerId));
}
protected function _getUpdateCondition($tableName)
{
return 'player_id = ' . $this->_db->quote($this->getExisting('player_id'));
}
/**
* @return XenForo_Model_Players
*/
protected function _getPlayerModel()
{
return $this->getModelFromCache('XenForo_Model_Player');
}
}
Template
Code:
<xen:foreach loop="$players" value="$player" i="$i">
<form action="{xen:link team/trainingPlayerUpdate}" method="post" class="Form">
<label for="ctrl_custom_title">{$player.name} {$player.surname}:</label>
<select name="training_group[]" class="textCtrl OptOut" id="ctrl_style_id" autofocus="true">
<option value="'1'">1</option>
<option value="'2'">2</option>
</optgroup>
</select>
{$player.training_group}
<input type="hidden" name="player_id" value="{$player.player_id}" /><br />
</xen:foreach>
<input type="submit" value="{xen:phrase save}" class="button primary" accesskey="s" />
<input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
</form>
Hope someone could help here
PS: All query are requesting all fields (select *) so no info missed there.
Regards