LB
Cadastro Mater/Detail - não grava os detalhes
Fechado
Pessoal se alguém puder dar uma olhada o que está errado em meu fonte, o cadastro máster/detalhe só está gravando o registro pai, os filhos (detalhes) não estão gravando e o sistema não retorna nenhum erro e anda dá mensagem que o registro foi gravado com sucesso.
Porém não está gravando no BD os detalhes vou colocar o fonte das classes modelos e o form de controle.
Classe Modelo Mater (Prova)
Classe Modelo Detail (QuestProva)
<? php e
/**
* QuestProva Active Record
* @author Leandro J N Barbosa
*/
class QuestProva extends TRecord
{
const TABLENAME = 'quest_prova';
const PRIMARYKEY= 'cod_quest_prov';
const IDPOLICY = 'serial'; // {max, serial}
private $prova;
private $questoes;
/**
* Constructor method
*/
public function __construct(2518 = NULL, $callObjectLoad = TRUE)
{
parent::__construct(2518, $callObjectLoad);
parent::addAttribute('cod_prova');
parent::addAttribute('cod_quest');
parent::addAttribute('valor');
}
/**
* Method set_prova
* Sample of usage: $quest_prova->prova = $object;
* @param $object Instance of Prova
*/
public function set_prova(Prova $object)
{
$this->prova = $object;
$this->cod_prova = $object->id;
}
/**
* Method get_prova
* Sample of usage: $quest_prova->prova->attribute;
* @returns Prova instance
*/
public function get_prova()
{
// loads the associated object
if (empty($this->prova))
$this->prova = new Prova($this->cod_prova);
// returns the associated object
return $this->prova;
}
/**
* Method set_questoes
* Sample of usage: $quest_prova->questoes = $object;
* @param $object Instance of Questoes
*/
public function set_questoes(Questoes $object)
{
$this->questoes = $object;
$this->cod_quest = $object->id;
}
/**
* Method get_questoes
* Sample of usage: $quest_prova->questoes->attribute;
* @returns Questoes instance
*/
public function get_questoes()
{
// loads the associated object
if (empty($this->questoes))
$this->questoes = new Questoes($this->cod_quest);
// returns the associated object
return $this->questoes;
}
/**
* Method get_questoes_eunicado
* Sample of usage: $conteudo->questoes->attribute;
* @returns Questoes->enunciado instance
*/
public function get_questoes_enunciado()
{
// loads the associated object
if (empty($this->Questoes))
$this->Questoes = new Questoes($this->cod_quest);
// returns the associated object
return $this->Questoes->enunciado;
}
}
?>
Formulário de Controle (ProvaForm)
<? php e
/**
* SaleForm Registration
* @author Leandro J N Barbosa
*/
class ProvaForm extends TPage
{
protected $form; // form
protected $formFields;
protected $dt_venda;
/**
* Class constructor
* Creates the page and the registration form
*/
function __construct()
{
parent::__construct();
// creates the form
$this->form = new TForm('form_prova');
$this->form->class = 'tform'; // CSS class
parent::include_css('app/resources/custom-frame.css');
$table_master = new TTable;
$table_master->width = '115%';
$table_master->addRowSet( new TLabel('Avaliação'), '', '')->class = 'tformtitle';
// add a table inside form
$table_general = new TTable;
$table_general-> width = '105%';
$tableQuestao = new TTable;
$tableQuestao-> width = '103%';
$frame_general = new TFrame;
$frame_general->setLegend('Prova');
$frame_general->style = 'background:whiteSmoke';
$frame_general->add($table_general);
$table_master->addRow()->addCell( $frame_general )->colspan=2;
$row = $table_master->addRow();
$row->addCell( $tableQuestao );
$this->form->add($table_master);
// master fields
$cod_prova = new TEntry('cod_prova');
$descricao = new TEntry('descricao');
$cod_cab = new TDBSeekButton('cod_cab', 'super_prova', $this->form->getName(), 'Cabecalho', 'descricao', 'cod_cab', 'cabecalho_descricao');
$cabecalho_descricao = new TEntry('cabecalho_descricao');
$observacao = new TText('observacao');
// detail fields
$cod_quest = new TDBSeekButton('cod_quest', 'super_prova', $this->form->getName(), 'Questoes', 'enunciado', 'cod_quest', 'questoes_enunciado');
$questoes_enunciado = new TText('questoes_enunciado');
$valor = new TEntry('valor');
$cod_prova->setSize(80);
$descricao->setSize(650);
$cabecalho_descricao->setSize(577);
$observacao->setSize(650,100);
$cod_quest->setSize(50);
$cod_cab->setSize(50);
$questoes_enunciado->setSize(625,100);
$cod_prova->setEditable(false);
$questoes_enunciado->setEditable(false);
$cabecalho_descricao->setEditable(false);
$descricao->addValidation('Descricao', new TRequiredValidator);
$cod_cab->addValidation('Cabecalho', new TRequiredValidator);
// prova
$table_general->addRowSet( new TLabel('Cód. Prova'), $cod_prova);
$table_general->addRowSet( $label_descricao = new TLabel('Descrição (*)'), $descricao );
$table_general->addRowSet( $label_cabecalho = new TLabel('Cabeçalho (*)'), array( $cod_cab, $cabecalho_descricao ) );
$table_general->addRowSet( new TLabel('Observação'), $observacao );
$label_descricao->setFontColor('#FF0000');
$label_cabecalho->setFontColor('#FF0000');
// questoes
$frame_quest = new TFrame();
$frame_quest->setLegend('Questões');
$row = $tableQuestao->addRow();
$row->addCell($frame_quest);
$add_quest = new TButton('add_quest');
$action_quest = new TAction(array($this, 'onQuestAdd'));
$add_quest->setAction($action_quest, 'Gravar');
$add_quest->setImage('fa:save');
$subtable_quest = new TTable;
$frame_quest->add($subtable_quest);
$subtable_quest->addRowSet( $label_quest = new TLabel('Questão (*)'), array($cod_quest,$questoes_enunciado) );
$subtable_quest->addRowSet( $label_valor = new TLabel('Valor (*)'), $valor );
$subtable_quest->addRowSet( $add_quest );
$label_quest->setFontColor('#FF0000');
$label_valor->setFontColor('#FF0000');
$this->quest_list = new TQuickGrid;
$this->quest_list->setHeight( 175 );
$this->quest_list->makeScrollable();
$this->quest_list->disableDefaultClick();
$this->quest_list->addQuickColumn('', 'edit', 'left', 50);
$this->quest_list->addQuickColumn('', 'delete', 'left', 50);
$this->quest_list->addQuickColumn('Cód. Questão', 'cod_quest', 'left', 50);
$this->quest_list->addQuickColumn('Enunciado', 'questoes_enunciado', 'left', 400);
$this->quest_list->addQuickColumn('Valor','valor', 'left', 80);
$this->quest_list->createModel();
$row = $tableQuestao->addRow();
$row->addCell($this->quest_list);
// create an action button (save)
$save_button=new TButton('save');
$save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
$save_button->setImage('ico_save.png');
// create an new button (edit with no parameters)
$new_button=new TButton('new');
$new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
$new_button->setImage('ico_new.png');
// define form fields
$this->formFields = array($cod_prova,$descricao, $cod_cab, $cabecalho_descricao, $observacao, $cod_quest, $questoes_enunciado, $valor, $add_quest, $save_button, $new_button);
$this->form->setFields( $this->formFields );
$table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
// create the page container
$container = new TVBox;
$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
$container->add($this->form);
parent::add($container);
}
/**
* Clear form
* @param $param URL parameters
*/
function onClear($param)
{
$this->form->clear();
TSession::setValue('quest_prova', array());
$this->onReload( $param );
}
/**
* Add a question into item list
* @param $param URL parameters
*/
public function onQuestAdd( $param )
{
try
{
TTransaction::open('super_prova');
$data = $this->form->getData();
if( (! $data->cod_quest) || (! $data->valor) )
throw new Exception('Os campos Questão e Valor são obrigatórios');
$questao = new Questoes($data->cod_quest);
$quest_prova = TSession::getValue('quest_prova');
$key = (int) $data->cod_quest;
$quest_prova[ $key ] = array('cod_quest' => $data->cod_quest,
'questoes_enunciado' => $data->questoes_enunciado,
'valor' => $data->valor);
TSession::setValue('quest_prova', $quest_prova);
// clear questao form fields after add
$data->cod_quest = '';
$data->questoes_enunciado = '';
$data->valor = '';
TTransaction::close();
$this->form->setData($data);
$this->onReload( $param ); // reload the sale items
}
catch (Exception $e)
{
$this->form->setData( $this->form->getData());
new TMessage('error', $e->getMessage());
}
}
/**
* Edit a question from item list
* @param $param URL parameters
*/
public function onEditItemQuest( $param )
{
$data = $this->form->getData();
// read session items
$quest_prova = TSession::getValue('quest_prova');
// get the session item
$quest_item = $quest_prova[ (int) $param['list_cod_quest'] ];
$data->cod_quest = $param['list_cod_quest'];
$data->questoes_enunciado = $quest_item['questoes_enunciado'];
$data->valor = $quest_item['valor'];
// fill product fields
$this->form->setData( $data );
$this->onReload( $param );
}
/**
* Delete a product from item list
* @param $param URL parameters
*/
public function onDeleteItem( $param )
{
$data = $this->form->getData();
$data->cod_quest = '';
$data->questoes_enunciado = '';
$data->valor = '';
// clear form data
$this->form->setData( $data );
// read session items
$quest_prova = TSession::getValue('quest_prova');
// delete the item from session
unset($quest_prova[ (int) $param['list_cod_quest'] ] );
TSession::setValue('quest_prova', $quest_prova);
// reload sale items
$this->onReload( $param );
}
/**
* Reload the item list
* @param $param URL parameters
*/
public function onReload($param)
{
// read session items
$quest_prova = TSession::getValue('quest_prova');
$this->quest_list->clear(); // clear product list
$data = $this->form->getData();
if ($quest_prova)
{
$cont = 1;
foreach ($quest_prova as $list_cod_quest => $list_quest)
{
$item_name = 'quest_' . $cont++;
$item = new StdClass;
// create action buttons
$action_del = new TAction(array($this, 'onDeleteItem'));
$action_del->setParameter('list_cod_quest', $list_cod_quest);
$action_edi = new TAction(array($this, 'onEditItemQuest'));
$action_edi->setParameter('list_cod_quest', $list_cod_quest);
$button_del = new TButton('delete_quest'.$cont);
$button_del->class = 'btn btn-default btn-sm';
$button_del->setAction( $action_del, '' );
$button_del->setImage('fa:trash-o red fa-lg');
$button_edi = new TButton('edit_quest'.$cont);
$button_edi->class = 'btn btn-default btn-sm';
$button_edi->setAction( $action_edi, '' );
$button_edi->setImage('fa:edit blue fa-lg');
$item->edit = $button_edi;
$item->delete = $button_del;
$this->formFields[ $item_name.'_edit' ] = $item->edit;
$this->formFields[ $item_name.'_delete' ] = $item->delete;
$item->cod_quest = $list_quest['cod_quest'];
$item->questoes_enunciado = $list_quest['questoes_enunciado'];
$item->valor = $list_quest['valor'];
$row = $this->quest_list->addItem( $item );
$row->onmouseover='';
$row->onmouseout='';
}
$this->form->setFields( $this->formFields );
}
$this->loaded = TRUE;
}
/**
* method onEdit()
* Executed whenever the user clicks at the edit button da datagrid
*/
function onEdit($param)
{
try
{
TTransaction::open('super_prova');
if (isset($param['key']))
{
$key = $param['key'];
$object = new Prova($key);
$quest_prova = $object->getQuestProva();
$session_items = array();
foreach( $quest_prova as $item )
{
$session_items[$item->cod_quest] = $item->toArray();
$session_items[$item->cod_quest]['cod_quest'] = $item->cod_quest;
$session_items[$item->cod_quest]['questoes_enunciado'] = $item->questoes_enunciado;
$session_items[$item->cod_quest]['valor'] = $item->valor;
}
TSession::setValue('quest_prova', $session_items);
$this->form->setData($object); // fill the form with the active record data
$this->onReload( $param ); // reload sale items list
TTransaction::close(); // close transaction
}
else
{
$this->form->clear();
TSession::setValue('quest_prova', null);
$this->onReload( $param );
}
}
catch (Exception $e) // in case of exception
{
new TMessage('error', 'Error ' . $e->getMessage());
TTransaction::rollback();
}
}
/**
* Save the sale and the sale items
*/
function onSave()
{
try
{
// open a transaction with database 'super_prova'
TTransaction::open('super_prova');
$Prova = $this->form->getData('Prova');
$this->form->validate(); // form validation
// get session questoes
$quest_prova = TSession::getValue('quest_prova');
if( $quest_prova )
{
foreach( $quest_prova as $quest_item )
{
$item = new QuestProva;
$item->cod_quest = $quest_item['cod_quest'];
$item->valor = $quest_item['valor'];
$Prova->addQuestProva($item);
}
}
$Prova->store(); // stores the object
$this->form->setData($Prova); // keep form data
TTransaction::close(); // close the transaction
new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
}
catch (Exception $e) // in case of exception
{
new TMessage('error', 'Error ' . $e->getMessage());
$this->form->setData( $this->form->getData() ); // keep form data
TTransaction::rollback();
}
}
/**
* Show the page
*/
public function show()
{
// check if the datagrid is already loaded
if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
Porém não está gravando no BD os detalhes vou colocar o fonte das classes modelos e o form de controle.
Classe Modelo Mater (Prova)
- <?php e
- /**
- * SaleForm Registration
- * @author Leandro J N Barbosa
- */
- class ProvaForm extends TPage
- {
- protected $form; // form
- protected $formFields;
- protected $dt_venda;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_prova');
- $this->form->class = 'tform'; // CSS class
- parent::include_css('app/resources/custom-frame.css');
- $table_master = new TTable;
- $table_master->width = '115%';
- $table_master->addRowSet( new TLabel('Avaliação'), '', '')->class = 'tformtitle';
- // add a table inside form
- $table_general = new TTable;
- $table_general-> width = '105%';
- $tableQuestao = new TTable;
- $tableQuestao-> width = '103%';
- $frame_general = new TFrame;
- $frame_general->setLegend('Prova');
- $frame_general->style = 'background:whiteSmoke';
- $frame_general->add($table_general);
- $table_master->addRow()->addCell( $frame_general )->colspan=2;
- $row = $table_master->addRow();
- $row->addCell( $tableQuestao );
- $this->form->add($table_master);
- // master fields
- $cod_prova = new TEntry('cod_prova');
- $descricao = new TEntry('descricao');
- $cod_cab = new ">TDBSeekButton('cod_cab', 'super_prova', $this->form->getName(), 'Cabecalho', 'descricao', 'cod_cab', 'cabecalho_descricao');
- $cabecalho_descricao = new TEntry('cabecalho_descricao');
- $observacao = new TText('observacao');
- // detail fields
- $cod_quest = new ">TDBSeekButton('cod_quest', 'super_prova', $this->form->getName(), 'Questoes', 'enunciado', 'cod_quest', 'questoes_enunciado');
- $questoes_enunciado = new TText('questoes_enunciado');
- $valor = new TEntry('valor');
- $cod_prova->setSize(80);
- $descricao->setSize(650);
- $cabecalho_descricao->setSize(577);
- $observacao->setSize(650,100);
- $cod_quest->setSize(50);
- $cod_cab->setSize(50);
- $questoes_enunciado->setSize(625,100);
- $cod_prova->setEditable(false);
- $questoes_enunciado->setEditable(false);
- $cabecalho_descricao->setEditable(false);
- $descricao->addValidation('Descricao', new TRequiredValidator);
- $cod_cab->addValidation('Cabecalho', new TRequiredValidator);
- // prova
- $table_general->addRowSet( new TLabel('Cód. Prova'), $cod_prova);
- $table_general->addRowSet( $label_descricao = new TLabel('Descrição (*)'), $descricao );
- $table_general->addRowSet( $label_cabecalho = new TLabel('Cabeçalho (*)'), array( $cod_cab, $cabecalho_descricao ) );
- $table_general->addRowSet( new TLabel('Observação'), $observacao );
- $label_descricao->setFontColor('#FF0000');
- $label_cabecalho->setFontColor('#FF0000');
- // questoes
- $frame_quest = new TFrame();
- $frame_quest->setLegend('Questões');
- $row = $tableQuestao->addRow();
- $row->addCell($frame_quest);
- $add_quest = new TButton('add_quest');
- $action_quest = new TAction(array($this, 'onQuestAdd'));
- $add_quest->setAction($action_quest, 'Gravar');
- $add_quest->setImage('fa:save');
- $subtable_quest = new TTable;
- $frame_quest->add($subtable_quest);
- $subtable_quest->addRowSet( $label_quest = new TLabel('Questão (*)'), array($cod_quest,$questoes_enunciado) );
- $subtable_quest->addRowSet( $label_valor = new TLabel('Valor (*)'), $valor );
- $subtable_quest->addRowSet( $add_quest );
- $label_quest->setFontColor('#FF0000');
- $label_valor->setFontColor('#FF0000');
- $this->quest_list = new TQuickGrid;
- $this->quest_list->setHeight( 175 );
- $this->quest_list->makeScrollable();
- $this->quest_list->disableDefaultClick();
- $this->quest_list->addQuickColumn('', 'edit', 'left', 50);
- $this->quest_list->addQuickColumn('', 'delete', 'left', 50);
- $this->quest_list->addQuickColumn('Cód. Questão', 'cod_quest', 'left', 50);
- $this->quest_list->addQuickColumn('Enunciado', 'questoes_enunciado', 'left', 400);
- $this->quest_list->addQuickColumn('Valor','valor', 'left', 80);
- $this->quest_list->createModel();
- $row = $tableQuestao->addRow();
- $row->addCell($this->quest_list);
- // create an action button (save)
- $save_button=new TButton('save');
- $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
- $save_button->setImage('ico_save.png');
- // create an new button (edit with no parameters)
- $new_button=new TButton('new');
- $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
- $new_button->setImage('ico_new.png');
- // define form fields
- $this->formFields = array($cod_prova,$descricao, $cod_cab, $cabecalho_descricao, $observacao, $cod_quest, $questoes_enunciado, $valor, $add_quest, $save_button, $new_button);
- $this->form->setFields( $this->formFields );
- $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
- // create the page container
- $container = new TVBox;
- $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Clear form
- * @param $param URL parameters
- */
- function onClear($param)
- {
- $this->form->clear();
- TSession::setValue('quest_prova', array());
- $this->onReload( $param );
- }
- /**
- * Add a question into item list
- * @param $param URL parameters
- */
- public function onQuestAdd( $param )
- {
- try
- {
- TTransaction::open('super_prova');
- $data = $this->form->getData();
- if( (! $data->cod_quest) || (! $data->valor) )
- throw new Exception('Os campos Questão e Valor são obrigatórios');
- $questao = new Questoes($data->cod_quest);
- $quest_prova = TSession::getValue('quest_prova');
- $key = (int) $data->cod_quest;
- $quest_prova[ $key ] = array('cod_quest' => $data->cod_quest,
- 'questoes_enunciado' => $data->questoes_enunciado,
- 'valor' => $data->valor);
- TSession::setValue('quest_prova', $quest_prova);
- // clear questao form fields after add
- $data->cod_quest = '';
- $data->questoes_enunciado = '';
- $data->valor = '';
- TTransaction::close();
- $this->form->setData($data);
- $this->onReload( $param ); // reload the sale items
- }
- catch (Exception $e)
- {
- $this->form->setData( $this->form->getData());
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Edit a question from item list
- * @param $param URL parameters
- */
- public function onEditItemQuest( $param )
- {
- $data = $this->form->getData();
- // read session items
- $quest_prova = TSession::getValue('quest_prova');
- // get the session item
- $quest_item = $quest_prova[ (int) $param['list_cod_quest'] ];
- $data->cod_quest = $param['list_cod_quest'];
- $data->questoes_enunciado = $quest_item['questoes_enunciado'];
- $data->valor = $quest_item['valor'];
- // fill product fields
- $this->form->setData( $data );
- $this->onReload( $param );
- }
- /**
- * Delete a product from item list
- * @param $param URL parameters
- */
- public function onDeleteItem( $param )
- {
- $data = $this->form->getData();
- $data->cod_quest = '';
- $data->questoes_enunciado = '';
- $data->valor = '';
- // clear form data
- $this->form->setData( $data );
- // read session items
- $quest_prova = TSession::getValue('quest_prova');
- // delete the item from session
- unset($quest_prova[ (int) $param['list_cod_quest'] ] );
- TSession::setValue('quest_prova', $quest_prova);
- // reload sale items
- $this->onReload( $param );
- }
- /**
- * Reload the item list
- * @param $param URL parameters
- */
- public function onReload($param)
- {
- // read session items
- $quest_prova = TSession::getValue('quest_prova');
- $this->quest_list->clear(); // clear product list
- $data = $this->form->getData();
- if ($quest_prova)
- {
- $cont = 1;
- foreach ($quest_prova as $list_cod_quest => $list_quest)
- {
- $item_name = 'quest_' . $cont++;
- $item = new StdClass;
- // create action buttons
- $action_del = new TAction(array($this, 'onDeleteItem'));
- $action_del->setParameter('list_cod_quest', $list_cod_quest);
- $action_edi = new TAction(array($this, 'onEditItemQuest'));
- $action_edi->setParameter('list_cod_quest', $list_cod_quest);
- $button_del = new TButton('delete_quest'.$cont);
- $button_del->class = 'btn btn-default btn-sm';
- $button_del->setAction( $action_del, '' );
- $button_del->setImage('fa:trash-o red fa-lg');
- $button_edi = new TButton('edit_quest'.$cont);
- $button_edi->class = 'btn btn-default btn-sm';
- $button_edi->setAction( $action_edi, '' );
- $button_edi->setImage('fa:edit blue fa-lg');
- $item->edit = $button_edi;
- $item->delete = $button_del;
- $this->formFields[ $item_name.'_edit' ] = $item->edit;
- $this->formFields[ $item_name.'_delete' ] = $item->delete;
- $item->cod_quest = $list_quest['cod_quest'];
- $item->questoes_enunciado = $list_quest['questoes_enunciado'];
- $item->valor = $list_quest['valor'];
- $row = $this->quest_list->addItem( $item );
- $row->onmouseover='';
- $row->onmouseout='';
- }
- $this->form->setFields( $this->formFields );
- }
- $this->loaded = TRUE;
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- TTransaction::open('super_prova');
- if (isset($param['key']))
- {
- $key = $param['key'];
- $object = new Prova($key);
- $quest_prova = $object->getQuestProva();
- $session_items = array();
- foreach( $quest_prova as $item )
- {
- $session_items[$item->cod_quest] = $item->toArray();
- $session_items[$item->cod_quest]['cod_quest'] = $item->cod_quest;
- $session_items[$item->cod_quest]['questoes_enunciado'] = $item->questoes_enunciado;
- $session_items[$item->cod_quest]['valor'] = $item->valor;
- }
- TSession::setValue('quest_prova', $session_items);
- $this->form->setData($object); // fill the form with the active record data
- $this->onReload( $param ); // reload sale items list
- TTransaction::close(); // close transaction
- }
- else
- {
- $this->form->clear();
- TSession::setValue('quest_prova', null);
- $this->onReload( $param );
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * Save the sale and the sale items
- */
- function onSave()
- {
- try
- {
- // open a transaction with database 'super_prova'
- TTransaction::open('super_prova');
- $Prova = $this->form->getData('Prova');
- $this->form->validate(); // form validation
- // get session questoes
- $quest_prova = TSession::getValue('quest_prova');
- if( $quest_prova )
- {
- foreach( $quest_prova as $quest_item )
- {
- $item = new QuestProva;
- $item->cod_quest = $quest_item['cod_quest'];
- $item->valor = $quest_item['valor'];
- $Prova->addQuestProva($item);
- }
- }
- $Prova->store(); // stores the object
- $this->form->setData($Prova); // keep form data
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback();
- }
- }
- /**
- * Show the page
- */
- public function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
- ?>
Classe Modelo Detail (QuestProva)
<? php e
/**
* QuestProva Active Record
* @author Leandro J N Barbosa
*/
class QuestProva extends TRecord
{
const TABLENAME = 'quest_prova';
const PRIMARYKEY= 'cod_quest_prov';
const IDPOLICY = 'serial'; // {max, serial}
private $prova;
private $questoes;
/**
* Constructor method
*/
public function __construct(2518 = NULL, $callObjectLoad = TRUE)
{
parent::__construct(2518, $callObjectLoad);
parent::addAttribute('cod_prova');
parent::addAttribute('cod_quest');
parent::addAttribute('valor');
}
/**
* Method set_prova
* Sample of usage: $quest_prova->prova = $object;
* @param $object Instance of Prova
*/
public function set_prova(Prova $object)
{
$this->prova = $object;
$this->cod_prova = $object->id;
}
/**
* Method get_prova
* Sample of usage: $quest_prova->prova->attribute;
* @returns Prova instance
*/
public function get_prova()
{
// loads the associated object
if (empty($this->prova))
$this->prova = new Prova($this->cod_prova);
// returns the associated object
return $this->prova;
}
/**
* Method set_questoes
* Sample of usage: $quest_prova->questoes = $object;
* @param $object Instance of Questoes
*/
public function set_questoes(Questoes $object)
{
$this->questoes = $object;
$this->cod_quest = $object->id;
}
/**
* Method get_questoes
* Sample of usage: $quest_prova->questoes->attribute;
* @returns Questoes instance
*/
public function get_questoes()
{
// loads the associated object
if (empty($this->questoes))
$this->questoes = new Questoes($this->cod_quest);
// returns the associated object
return $this->questoes;
}
/**
* Method get_questoes_eunicado
* Sample of usage: $conteudo->questoes->attribute;
* @returns Questoes->enunciado instance
*/
public function get_questoes_enunciado()
{
// loads the associated object
if (empty($this->Questoes))
$this->Questoes = new Questoes($this->cod_quest);
// returns the associated object
return $this->Questoes->enunciado;
}
}
?>
Formulário de Controle (ProvaForm)
<? php e
/**
* SaleForm Registration
* @author Leandro J N Barbosa
*/
class ProvaForm extends TPage
{
protected $form; // form
protected $formFields;
protected $dt_venda;
/**
* Class constructor
* Creates the page and the registration form
*/
function __construct()
{
parent::__construct();
// creates the form
$this->form = new TForm('form_prova');
$this->form->class = 'tform'; // CSS class
parent::include_css('app/resources/custom-frame.css');
$table_master = new TTable;
$table_master->width = '115%';
$table_master->addRowSet( new TLabel('Avaliação'), '', '')->class = 'tformtitle';
// add a table inside form
$table_general = new TTable;
$table_general-> width = '105%';
$tableQuestao = new TTable;
$tableQuestao-> width = '103%';
$frame_general = new TFrame;
$frame_general->setLegend('Prova');
$frame_general->style = 'background:whiteSmoke';
$frame_general->add($table_general);
$table_master->addRow()->addCell( $frame_general )->colspan=2;
$row = $table_master->addRow();
$row->addCell( $tableQuestao );
$this->form->add($table_master);
// master fields
$cod_prova = new TEntry('cod_prova');
$descricao = new TEntry('descricao');
$cod_cab = new TDBSeekButton('cod_cab', 'super_prova', $this->form->getName(), 'Cabecalho', 'descricao', 'cod_cab', 'cabecalho_descricao');
$cabecalho_descricao = new TEntry('cabecalho_descricao');
$observacao = new TText('observacao');
// detail fields
$cod_quest = new TDBSeekButton('cod_quest', 'super_prova', $this->form->getName(), 'Questoes', 'enunciado', 'cod_quest', 'questoes_enunciado');
$questoes_enunciado = new TText('questoes_enunciado');
$valor = new TEntry('valor');
$cod_prova->setSize(80);
$descricao->setSize(650);
$cabecalho_descricao->setSize(577);
$observacao->setSize(650,100);
$cod_quest->setSize(50);
$cod_cab->setSize(50);
$questoes_enunciado->setSize(625,100);
$cod_prova->setEditable(false);
$questoes_enunciado->setEditable(false);
$cabecalho_descricao->setEditable(false);
$descricao->addValidation('Descricao', new TRequiredValidator);
$cod_cab->addValidation('Cabecalho', new TRequiredValidator);
// prova
$table_general->addRowSet( new TLabel('Cód. Prova'), $cod_prova);
$table_general->addRowSet( $label_descricao = new TLabel('Descrição (*)'), $descricao );
$table_general->addRowSet( $label_cabecalho = new TLabel('Cabeçalho (*)'), array( $cod_cab, $cabecalho_descricao ) );
$table_general->addRowSet( new TLabel('Observação'), $observacao );
$label_descricao->setFontColor('#FF0000');
$label_cabecalho->setFontColor('#FF0000');
// questoes
$frame_quest = new TFrame();
$frame_quest->setLegend('Questões');
$row = $tableQuestao->addRow();
$row->addCell($frame_quest);
$add_quest = new TButton('add_quest');
$action_quest = new TAction(array($this, 'onQuestAdd'));
$add_quest->setAction($action_quest, 'Gravar');
$add_quest->setImage('fa:save');
$subtable_quest = new TTable;
$frame_quest->add($subtable_quest);
$subtable_quest->addRowSet( $label_quest = new TLabel('Questão (*)'), array($cod_quest,$questoes_enunciado) );
$subtable_quest->addRowSet( $label_valor = new TLabel('Valor (*)'), $valor );
$subtable_quest->addRowSet( $add_quest );
$label_quest->setFontColor('#FF0000');
$label_valor->setFontColor('#FF0000');
$this->quest_list = new TQuickGrid;
$this->quest_list->setHeight( 175 );
$this->quest_list->makeScrollable();
$this->quest_list->disableDefaultClick();
$this->quest_list->addQuickColumn('', 'edit', 'left', 50);
$this->quest_list->addQuickColumn('', 'delete', 'left', 50);
$this->quest_list->addQuickColumn('Cód. Questão', 'cod_quest', 'left', 50);
$this->quest_list->addQuickColumn('Enunciado', 'questoes_enunciado', 'left', 400);
$this->quest_list->addQuickColumn('Valor','valor', 'left', 80);
$this->quest_list->createModel();
$row = $tableQuestao->addRow();
$row->addCell($this->quest_list);
// create an action button (save)
$save_button=new TButton('save');
$save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
$save_button->setImage('ico_save.png');
// create an new button (edit with no parameters)
$new_button=new TButton('new');
$new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
$new_button->setImage('ico_new.png');
// define form fields
$this->formFields = array($cod_prova,$descricao, $cod_cab, $cabecalho_descricao, $observacao, $cod_quest, $questoes_enunciado, $valor, $add_quest, $save_button, $new_button);
$this->form->setFields( $this->formFields );
$table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
// create the page container
$container = new TVBox;
$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
$container->add($this->form);
parent::add($container);
}
/**
* Clear form
* @param $param URL parameters
*/
function onClear($param)
{
$this->form->clear();
TSession::setValue('quest_prova', array());
$this->onReload( $param );
}
/**
* Add a question into item list
* @param $param URL parameters
*/
public function onQuestAdd( $param )
{
try
{
TTransaction::open('super_prova');
$data = $this->form->getData();
if( (! $data->cod_quest) || (! $data->valor) )
throw new Exception('Os campos Questão e Valor são obrigatórios');
$questao = new Questoes($data->cod_quest);
$quest_prova = TSession::getValue('quest_prova');
$key = (int) $data->cod_quest;
$quest_prova[ $key ] = array('cod_quest' => $data->cod_quest,
'questoes_enunciado' => $data->questoes_enunciado,
'valor' => $data->valor);
TSession::setValue('quest_prova', $quest_prova);
// clear questao form fields after add
$data->cod_quest = '';
$data->questoes_enunciado = '';
$data->valor = '';
TTransaction::close();
$this->form->setData($data);
$this->onReload( $param ); // reload the sale items
}
catch (Exception $e)
{
$this->form->setData( $this->form->getData());
new TMessage('error', $e->getMessage());
}
}
/**
* Edit a question from item list
* @param $param URL parameters
*/
public function onEditItemQuest( $param )
{
$data = $this->form->getData();
// read session items
$quest_prova = TSession::getValue('quest_prova');
// get the session item
$quest_item = $quest_prova[ (int) $param['list_cod_quest'] ];
$data->cod_quest = $param['list_cod_quest'];
$data->questoes_enunciado = $quest_item['questoes_enunciado'];
$data->valor = $quest_item['valor'];
// fill product fields
$this->form->setData( $data );
$this->onReload( $param );
}
/**
* Delete a product from item list
* @param $param URL parameters
*/
public function onDeleteItem( $param )
{
$data = $this->form->getData();
$data->cod_quest = '';
$data->questoes_enunciado = '';
$data->valor = '';
// clear form data
$this->form->setData( $data );
// read session items
$quest_prova = TSession::getValue('quest_prova');
// delete the item from session
unset($quest_prova[ (int) $param['list_cod_quest'] ] );
TSession::setValue('quest_prova', $quest_prova);
// reload sale items
$this->onReload( $param );
}
/**
* Reload the item list
* @param $param URL parameters
*/
public function onReload($param)
{
// read session items
$quest_prova = TSession::getValue('quest_prova');
$this->quest_list->clear(); // clear product list
$data = $this->form->getData();
if ($quest_prova)
{
$cont = 1;
foreach ($quest_prova as $list_cod_quest => $list_quest)
{
$item_name = 'quest_' . $cont++;
$item = new StdClass;
// create action buttons
$action_del = new TAction(array($this, 'onDeleteItem'));
$action_del->setParameter('list_cod_quest', $list_cod_quest);
$action_edi = new TAction(array($this, 'onEditItemQuest'));
$action_edi->setParameter('list_cod_quest', $list_cod_quest);
$button_del = new TButton('delete_quest'.$cont);
$button_del->class = 'btn btn-default btn-sm';
$button_del->setAction( $action_del, '' );
$button_del->setImage('fa:trash-o red fa-lg');
$button_edi = new TButton('edit_quest'.$cont);
$button_edi->class = 'btn btn-default btn-sm';
$button_edi->setAction( $action_edi, '' );
$button_edi->setImage('fa:edit blue fa-lg');
$item->edit = $button_edi;
$item->delete = $button_del;
$this->formFields[ $item_name.'_edit' ] = $item->edit;
$this->formFields[ $item_name.'_delete' ] = $item->delete;
$item->cod_quest = $list_quest['cod_quest'];
$item->questoes_enunciado = $list_quest['questoes_enunciado'];
$item->valor = $list_quest['valor'];
$row = $this->quest_list->addItem( $item );
$row->onmouseover='';
$row->onmouseout='';
}
$this->form->setFields( $this->formFields );
}
$this->loaded = TRUE;
}
/**
* method onEdit()
* Executed whenever the user clicks at the edit button da datagrid
*/
function onEdit($param)
{
try
{
TTransaction::open('super_prova');
if (isset($param['key']))
{
$key = $param['key'];
$object = new Prova($key);
$quest_prova = $object->getQuestProva();
$session_items = array();
foreach( $quest_prova as $item )
{
$session_items[$item->cod_quest] = $item->toArray();
$session_items[$item->cod_quest]['cod_quest'] = $item->cod_quest;
$session_items[$item->cod_quest]['questoes_enunciado'] = $item->questoes_enunciado;
$session_items[$item->cod_quest]['valor'] = $item->valor;
}
TSession::setValue('quest_prova', $session_items);
$this->form->setData($object); // fill the form with the active record data
$this->onReload( $param ); // reload sale items list
TTransaction::close(); // close transaction
}
else
{
$this->form->clear();
TSession::setValue('quest_prova', null);
$this->onReload( $param );
}
}
catch (Exception $e) // in case of exception
{
new TMessage('error', 'Error ' . $e->getMessage());
TTransaction::rollback();
}
}
/**
* Save the sale and the sale items
*/
function onSave()
{
try
{
// open a transaction with database 'super_prova'
TTransaction::open('super_prova');
$Prova = $this->form->getData('Prova');
$this->form->validate(); // form validation
// get session questoes
$quest_prova = TSession::getValue('quest_prova');
if( $quest_prova )
{
foreach( $quest_prova as $quest_item )
{
$item = new QuestProva;
$item->cod_quest = $quest_item['cod_quest'];
$item->valor = $quest_item['valor'];
$Prova->addQuestProva($item);
}
}
$Prova->store(); // stores the object
$this->form->setData($Prova); // keep form data
TTransaction::close(); // close the transaction
new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
}
catch (Exception $e) // in case of exception
{
new TMessage('error', 'Error ' . $e->getMessage());
$this->form->setData( $this->form->getData() ); // keep form data
TTransaction::rollback();
}
}
/**
* Show the page
*/
public function show()
{
// check if the datagrid is already loaded
if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
Poste o modelo do master também...
Classe Master - é a Prova segue abaixo, já tinha ido mas no post acima ficou zuado
<? php e
/**
* Prova Active Record
* @author Leandro J N Barbosa
*/
class Prova extends TRecord
{
const TABLENAME = 'prova';
const PRIMARYKEY= 'cod_prova';
const IDPOLICY = 'serial'; // {max, serial}
private $cabecalho;
private $questprova;
/**
* Constructor method
*/
public function __construct($id = NULL, $callObjectLoad = TRUE)
{
parent::__construct($id, $callObjectLoad);
parent::addAttribute('descricao');
parent::addAttribute('cod_cab');
parent::addAttribute('observacao');
}
/**
* Method set_cabecalho
* Sample of usage: $prova->cabecalho = $object;
* @param $object Instance of Cabecalho
*/
public function set_cabecalho(Cabecalho $object)
{
$this->cabecalho = $object;
$this->cod_cab = $object->id;
}
/**
* Method get_cabecalho
* Sample of usage: $prova->cabecalho->attribute;
* @returns Cabecalho instance
*/
public function get_cabecalho()
{
// loads the associated object
if (empty($this->cabecalho))
$this->cabecalho = new Cabecalho($this->cod_cab);
// returns the associated object
return $this->cabecalho;
}
/**
* Method get_cabecalho_descricao
* Sample of usage: $prova->cabecalho->descricao;
* @returns Cabecalho Descricao instance
*/
public function get_cabecalho_descricao()
{
// loads the associated object
if (empty($this->Cabecalho))
$this->Cabecalho = new Cabecalho($this->cod_cab);
// returns the associated object
return $this->Cabecalho->descricao;
}
/**
* Method addQuestProva
* Add a QuestItem to the Prova
* @param $object Instance of QuestItem
*/
public function addQuestProva(QuestProva $object)
{
$this->Quest_Provas[] = $object;
}
/**
* Method getQuestProva
* Return the Prova QuestProva
* @return Collection of QuestProva
*/
public function getQuestProva()
{
return $this->Quest_Provas;
}
/**
* Reset aggregates
*/
public function clearParts()
{
$this->Quest_Provas = array();
}
/**
* Load the object and its aggregates
* @param $id object ID
*/
public function load($id)
{
// load the related QuestProva objects
$repository = new TRepository('QuestProva');
$criteria = new TCriteria;
$criteria->add(new TFilter('cod_prova', '=', $id));
$this->Quest_Provas = $repository->load($criteria);
// load the object itself
return parent::load($id);
}
/**
* Store the object and its aggregates
*/
public function store()
{
// store the object itself
parent::store();
// delete the related QuestProva objects
$criteria = new TCriteria;
$criteria->add(new TFilter('cod_prova', '=', $this->cod_prova));
$repository = new TRepository('QuestProva');
$repository->delete($criteria);
// store the related QuestProva objects
if ($this->Quest_Provas)
{
foreach ($this->Quest_Provas as $quest_prova)
{
unset($quest_prova->cod_prova);
$quest_prova->cod_prova = $this->cod_prova;
$quest_prova->store();
}
}
}
/**
* Delete the object and its aggregates
* @param $id object ID
*/
public function delete($id = NULL)
{
// delete the related QuestProva objects
$id = isset($id) ? $id : $this->cod_prova;
$repository = new TRepository('QuestProva');
$criteria = new TCriteria;
$criteria->add(new TFilter('cod_prova', '=', $id));
$repository->delete($criteria);
// delete the object itself
parent::delete($id);
}
}
?>
Classe Detail - QuestProva
Por fim a classe de controle - ProvaForm
<? php e
?>
Habilita os logs do sql logo após o TTransaction::open na onSave:
Já habilitei onde o arquivo de log é gerado?
Nataniel, consegui ver o log na tela mesmo mas tava tudo certo no sql, dai alterei função onSave para o código conforme abaixo e agora está gravando.
?>