Cadastro Mater/Detail - não grava os detalhes 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 M...
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)
 
  1. <?php e
  2. /**
  3. * SaleForm Registration
  4. * @author Leandro J N Barbosa
  5. */
  6. class ProvaForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $formFields;
  10. protected $dt_venda;
  11. /**
  12. * Class constructor
  13. * Creates the page and the registration form
  14. */
  15. function __construct()
  16. {
  17. parent::__construct();
  18. // creates the form
  19. $this->form = new TForm('form_prova');
  20. $this->form->class = 'tform'; // CSS class
  21. parent::include_css('app/resources/custom-frame.css');
  22. $table_master = new TTable;
  23. $table_master->width = '115%';
  24. $table_master->addRowSet( new TLabel('Avaliação'), '', '')->class = 'tformtitle';
  25. // add a table inside form
  26. $table_general = new TTable;
  27. $table_general-> width = '105%';
  28. $tableQuestao = new TTable;
  29. $tableQuestao-> width = '103%';
  30. $frame_general = new TFrame;
  31. $frame_general->setLegend('Prova');
  32. $frame_general->style = 'background:whiteSmoke';
  33. $frame_general->add($table_general);
  34. $table_master->addRow()->addCell( $frame_general )->colspan=2;
  35. $row = $table_master->addRow();
  36. $row->addCell( $tableQuestao );
  37. $this->form->add($table_master);
  38. // master fields
  39. $cod_prova = new TEntry('cod_prova');
  40. $descricao = new TEntry('descricao');
  41. $cod_cab = new ">TDBSeekButton('cod_cab', 'super_prova', $this->form->getName(), 'Cabecalho', 'descricao', 'cod_cab', 'cabecalho_descricao');
  42. $cabecalho_descricao = new TEntry('cabecalho_descricao');
  43. $observacao = new TText('observacao');
  44. // detail fields
  45. $cod_quest = new ">TDBSeekButton('cod_quest', 'super_prova', $this->form->getName(), 'Questoes', 'enunciado', 'cod_quest', 'questoes_enunciado');
  46. $questoes_enunciado = new TText('questoes_enunciado');
  47. $valor = new TEntry('valor');
  48. $cod_prova->setSize(80);
  49. $descricao->setSize(650);
  50. $cabecalho_descricao->setSize(577);
  51. $observacao->setSize(650,100);
  52. $cod_quest->setSize(50);
  53. $cod_cab->setSize(50);
  54. $questoes_enunciado->setSize(625,100);
  55. $cod_prova->setEditable(false);
  56. $questoes_enunciado->setEditable(false);
  57. $cabecalho_descricao->setEditable(false);
  58. $descricao->addValidation('Descricao', new TRequiredValidator);
  59. $cod_cab->addValidation('Cabecalho', new TRequiredValidator);
  60. // prova
  61. $table_general->addRowSet( new TLabel('Cód. Prova'), $cod_prova);
  62. $table_general->addRowSet( $label_descricao = new TLabel('Descrição (*)'), $descricao );
  63. $table_general->addRowSet( $label_cabecalho = new TLabel('Cabeçalho (*)'), array( $cod_cab, $cabecalho_descricao ) );
  64. $table_general->addRowSet( new TLabel('Observação'), $observacao );
  65. $label_descricao->setFontColor('#FF0000');
  66. $label_cabecalho->setFontColor('#FF0000');
  67. // questoes
  68. $frame_quest = new TFrame();
  69. $frame_quest->setLegend('Questões');
  70. $row = $tableQuestao->addRow();
  71. $row->addCell($frame_quest);
  72. $add_quest = new TButton('add_quest');
  73. $action_quest = new TAction(array($this, 'onQuestAdd'));
  74. $add_quest->setAction($action_quest, 'Gravar');
  75. $add_quest->setImage('fa:save');
  76. $subtable_quest = new TTable;
  77. $frame_quest->add($subtable_quest);
  78. $subtable_quest->addRowSet( $label_quest = new TLabel('Questão (*)'), array($cod_quest,$questoes_enunciado) );
  79. $subtable_quest->addRowSet( $label_valor = new TLabel('Valor (*)'), $valor );
  80. $subtable_quest->addRowSet( $add_quest );
  81. $label_quest->setFontColor('#FF0000');
  82. $label_valor->setFontColor('#FF0000');
  83. $this->quest_list = new TQuickGrid;
  84. $this->quest_list->setHeight( 175 );
  85. $this->quest_list->makeScrollable();
  86. $this->quest_list->disableDefaultClick();
  87. $this->quest_list->addQuickColumn('', 'edit', 'left', 50);
  88. $this->quest_list->addQuickColumn('', 'delete', 'left', 50);
  89. $this->quest_list->addQuickColumn('Cód. Questão', 'cod_quest', 'left', 50);
  90. $this->quest_list->addQuickColumn('Enunciado', 'questoes_enunciado', 'left', 400);
  91. $this->quest_list->addQuickColumn('Valor','valor', 'left', 80);
  92. $this->quest_list->createModel();
  93. $row = $tableQuestao->addRow();
  94. $row->addCell($this->quest_list);
  95. // create an action button (save)
  96. $save_button=new TButton('save');
  97. $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
  98. $save_button->setImage('ico_save.png');
  99. // create an new button (edit with no parameters)
  100. $new_button=new TButton('new');
  101. $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
  102. $new_button->setImage('ico_new.png');
  103. // define form fields
  104. $this->formFields = array($cod_prova,$descricao, $cod_cab, $cabecalho_descricao, $observacao, $cod_quest, $questoes_enunciado, $valor, $add_quest, $save_button, $new_button);
  105. $this->form->setFields( $this->formFields );
  106. $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
  107. // create the page container
  108. $container = new TVBox;
  109. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  110. $container->add($this->form);
  111. parent::add($container);
  112. }
  113. /**
  114. * Clear form
  115. * @param $param URL parameters
  116. */
  117. function onClear($param)
  118. {
  119. $this->form->clear();
  120. TSession::setValue('quest_prova', array());
  121. $this->onReload( $param );
  122. }
  123. /**
  124. * Add a question into item list
  125. * @param $param URL parameters
  126. */
  127. public function onQuestAdd( $param )
  128. {
  129. try
  130. {
  131. TTransaction::open('super_prova');
  132. $data = $this->form->getData();
  133. if( (! $data->cod_quest) || (! $data->valor) )
  134. throw new Exception('Os campos Questão e Valor são obrigatórios');
  135. $questao = new Questoes($data->cod_quest);
  136. $quest_prova = TSession::getValue('quest_prova');
  137. $key = (int) $data->cod_quest;
  138. $quest_prova[ $key ] = array('cod_quest' => $data->cod_quest,
  139. 'questoes_enunciado' => $data->questoes_enunciado,
  140. 'valor' => $data->valor);
  141. TSession::setValue('quest_prova', $quest_prova);
  142. // clear questao form fields after add
  143. $data->cod_quest = '';
  144. $data->questoes_enunciado = '';
  145. $data->valor = '';
  146. TTransaction::close();
  147. $this->form->setData($data);
  148. $this->onReload( $param ); // reload the sale items
  149. }
  150. catch (Exception $e)
  151. {
  152. $this->form->setData( $this->form->getData());
  153. new TMessage('error', $e->getMessage());
  154. }
  155. }
  156. /**
  157. * Edit a question from item list
  158. * @param $param URL parameters
  159. */
  160. public function onEditItemQuest( $param )
  161. {
  162. $data = $this->form->getData();
  163. // read session items
  164. $quest_prova = TSession::getValue('quest_prova');
  165. // get the session item
  166. $quest_item = $quest_prova[ (int) $param['list_cod_quest'] ];
  167. $data->cod_quest = $param['list_cod_quest'];
  168. $data->questoes_enunciado = $quest_item['questoes_enunciado'];
  169. $data->valor = $quest_item['valor'];
  170. // fill product fields
  171. $this->form->setData( $data );
  172. $this->onReload( $param );
  173. }
  174. /**
  175. * Delete a product from item list
  176. * @param $param URL parameters
  177. */
  178. public function onDeleteItem( $param )
  179. {
  180. $data = $this->form->getData();
  181. $data->cod_quest = '';
  182. $data->questoes_enunciado = '';
  183. $data->valor = '';
  184. // clear form data
  185. $this->form->setData( $data );
  186. // read session items
  187. $quest_prova = TSession::getValue('quest_prova');
  188. // delete the item from session
  189. unset($quest_prova[ (int) $param['list_cod_quest'] ] );
  190. TSession::setValue('quest_prova', $quest_prova);
  191. // reload sale items
  192. $this->onReload( $param );
  193. }
  194. /**
  195. * Reload the item list
  196. * @param $param URL parameters
  197. */
  198. public function onReload($param)
  199. {
  200. // read session items
  201. $quest_prova = TSession::getValue('quest_prova');
  202. $this->quest_list->clear(); // clear product list
  203. $data = $this->form->getData();
  204. if ($quest_prova)
  205. {
  206. $cont = 1;
  207. foreach ($quest_prova as $list_cod_quest => $list_quest)
  208. {
  209. $item_name = 'quest_' . $cont++;
  210. $item = new StdClass;
  211. // create action buttons
  212. $action_del = new TAction(array($this, 'onDeleteItem'));
  213. $action_del->setParameter('list_cod_quest', $list_cod_quest);
  214. $action_edi = new TAction(array($this, 'onEditItemQuest'));
  215. $action_edi->setParameter('list_cod_quest', $list_cod_quest);
  216. $button_del = new TButton('delete_quest'.$cont);
  217. $button_del->class = 'btn btn-default btn-sm';
  218. $button_del->setAction( $action_del, '' );
  219. $button_del->setImage('fa:trash-o red fa-lg');
  220. $button_edi = new TButton('edit_quest'.$cont);
  221. $button_edi->class = 'btn btn-default btn-sm';
  222. $button_edi->setAction( $action_edi, '' );
  223. $button_edi->setImage('fa:edit blue fa-lg');
  224. $item->edit = $button_edi;
  225. $item->delete = $button_del;
  226. $this->formFields[ $item_name.'_edit' ] = $item->edit;
  227. $this->formFields[ $item_name.'_delete' ] = $item->delete;
  228. $item->cod_quest = $list_quest['cod_quest'];
  229. $item->questoes_enunciado = $list_quest['questoes_enunciado'];
  230. $item->valor = $list_quest['valor'];
  231. $row = $this->quest_list->addItem( $item );
  232. $row->onmouseover='';
  233. $row->onmouseout='';
  234. }
  235. $this->form->setFields( $this->formFields );
  236. }
  237. $this->loaded = TRUE;
  238. }
  239. /**
  240. * method onEdit()
  241. * Executed whenever the user clicks at the edit button da datagrid
  242. */
  243. function onEdit($param)
  244. {
  245. try
  246. {
  247. TTransaction::open('super_prova');
  248. if (isset($param['key']))
  249. {
  250. $key = $param['key'];
  251. $object = new Prova($key);
  252. $quest_prova = $object->getQuestProva();
  253. $session_items = array();
  254. foreach( $quest_prova as $item )
  255. {
  256. $session_items[$item->cod_quest] = $item->toArray();
  257. $session_items[$item->cod_quest]['cod_quest'] = $item->cod_quest;
  258. $session_items[$item->cod_quest]['questoes_enunciado'] = $item->questoes_enunciado;
  259. $session_items[$item->cod_quest]['valor'] = $item->valor;
  260. }
  261. TSession::setValue('quest_prova', $session_items);
  262. $this->form->setData($object); // fill the form with the active record data
  263. $this->onReload( $param ); // reload sale items list
  264. TTransaction::close(); // close transaction
  265. }
  266. else
  267. {
  268. $this->form->clear();
  269. TSession::setValue('quest_prova', null);
  270. $this->onReload( $param );
  271. }
  272. }
  273. catch (Exception $e) // in case of exception
  274. {
  275. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  276. TTransaction::rollback();
  277. }
  278. }
  279. /**
  280. * Save the sale and the sale items
  281. */
  282. function onSave()
  283. {
  284. try
  285. {
  286. // open a transaction with database 'super_prova'
  287. TTransaction::open('super_prova');
  288. $Prova = $this->form->getData('Prova');
  289. $this->form->validate(); // form validation
  290. // get session questoes
  291. $quest_prova = TSession::getValue('quest_prova');
  292. if( $quest_prova )
  293. {
  294. foreach( $quest_prova as $quest_item )
  295. {
  296. $item = new QuestProva;
  297. $item->cod_quest = $quest_item['cod_quest'];
  298. $item->valor = $quest_item['valor'];
  299. $Prova->addQuestProva($item);
  300. }
  301. }
  302. $Prova->store(); // stores the object
  303. $this->form->setData($Prova); // keep form data
  304. TTransaction::close(); // close the transaction
  305. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  306. }
  307. catch (Exception $e) // in case of exception
  308. {
  309. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  310. $this->form->setData( $this->form->getData() ); // keep form data
  311. TTransaction::rollback();
  312. }
  313. }
  314. /**
  315. * Show the page
  316. */
  317. public function show()
  318. {
  319. // check if the datagrid is already loaded
  320. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  321. {
  322. $this->onReload( func_get_arg(0) );
  323. }
  324. parent::show();
  325. }
  326. }
  327. ?>


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') )

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (7)


NR

Poste o modelo do master também...
LB

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);
}

}
?>
LB

Classe Detail - QuestProva
 
  1. <?php e
 
  1. <?php
  2. /**
  3. * QuestProva Active Record
  4. * @author Leandro J N Barbosa
  5. */
  6. class QuestProva extends TRecord
  7. {
  8. const TABLENAME = 'quest_prova';
  9. const PRIMARYKEY= 'cod_quest_prov';
  10. const IDPOLICY = 'serial'; // {max, serial}
  11. private $prova;
  12. private $questoes;
  13. /**
  14. * Constructor method
  15. */
  16. public function __construct($id = NULL, $callObjectLoad = TRUE)
  17. {
  18. parent::__construct($id, $callObjectLoad);
  19. parent::addAttribute('cod_prova');
  20. parent::addAttribute('cod_quest');
  21. parent::addAttribute('valor');
  22. }
  23. /**
  24. * Method set_prova
  25. * Sample of usage: $quest_prova->prova = $object;
  26. * @param $object Instance of Prova
  27. */
  28. public function set_prova(Prova $object)
  29. {
  30. $this->prova = $object;
  31. $this->cod_prova = $object->id;
  32. }
  33. /**
  34. * Method get_prova
  35. * Sample of usage: $quest_prova->prova->attribute;
  36. * @returns Prova instance
  37. */
  38. public function get_prova()
  39. {
  40. // loads the associated object
  41. if (empty($this->prova))
  42. $this->prova = new Prova($this->cod_prova);
  43. // returns the associated object
  44. return $this->prova;
  45. }
  46. /**
  47. * Method set_questoes
  48. * Sample of usage: $quest_prova->questoes = $object;
  49. * @param $object Instance of Questoes
  50. */
  51. public function set_questoes(Questoes $object)
  52. {
  53. $this->questoes = $object;
  54. $this->cod_quest = $object->id;
  55. }
  56. /**
  57. * Method get_questoes
  58. * Sample of usage: $quest_prova->questoes->attribute;
  59. * @returns Questoes instance
  60. */
  61. public function get_questoes()
  62. {
  63. // loads the associated object
  64. if (empty($this->questoes))
  65. $this->questoes = new Questoes($this->cod_quest);
  66. // returns the associated object
  67. return $this->questoes;
  68. }
  69. /**
  70. * Method get_questoes_eunicado
  71. * Sample of usage: $conteudo->questoes->attribute;
  72. * @returns Questoes->enunciado instance
  73. */
  74. public function get_questoes_enunciado()
  75. {
  76. // loads the associated object
  77. if (empty($this->Questoes))
  78. $this->Questoes = new Questoes($this->cod_quest);
  79. // returns the associated object
  80. return $this->Questoes->enunciado;
  81. }
  82. }
  83. ?>
LB

Por fim a classe de controle - ProvaForm

<? php e
 
  1. <?php
  2. /**
  3. * SaleForm Registration
  4. * @author Leandro J N Barbosa
  5. */
  6. class ProvaForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $formFields;
  10. protected $dt_venda;
  11. /**
  12. * Class constructor
  13. * Creates the page and the registration form
  14. */
  15. function __construct()
  16. {
  17. parent::__construct();
  18. // creates the form
  19. $this->form = new TForm('form_prova');
  20. $this->form->class = 'tform'; // CSS class
  21. parent::include_css('app/resources/custom-frame.css');
  22. $table_master = new TTable;
  23. $table_master->width = '115%';
  24. $table_master->addRowSet( new TLabel('Avaliação'), '', '')->class = 'tformtitle';
  25. // add a table inside form
  26. $table_general = new TTable;
  27. $table_general-> width = '105%';
  28. $tableQuestao = new TTable;
  29. $tableQuestao-> width = '103%';
  30. $frame_general = new TFrame;
  31. $frame_general->setLegend('Prova');
  32. $frame_general->style = 'background:whiteSmoke';
  33. $frame_general->add($table_general);
  34. $table_master->addRow()->addCell( $frame_general )->colspan=2;
  35. $row = $table_master->addRow();
  36. $row->addCell( $tableQuestao );
  37. $this->form->add($table_master);
  38. // master fields
  39. $cod_prova = new TEntry('cod_prova');
  40. $descricao = new TEntry('descricao');
  41. $cod_cab = new ">TDBSeekButton('cod_cab', 'super_prova', $this->form->getName(), 'Cabecalho', 'descricao', 'cod_cab', 'cabecalho_descricao');
  42. $cabecalho_descricao = new TEntry('cabecalho_descricao');
  43. $observacao = new TText('observacao');
  44. // detail fields
  45. $cod_quest = new ">TDBSeekButton('cod_quest', 'super_prova', $this->form->getName(), 'Questoes', 'enunciado', 'cod_quest', 'questoes_enunciado');
  46. $questoes_enunciado = new TText('questoes_enunciado');
  47. $valor = new TEntry('valor');
  48. $cod_prova->setSize(80);
  49. $descricao->setSize(650);
  50. $cabecalho_descricao->setSize(577);
  51. $observacao->setSize(650,100);
  52. $cod_quest->setSize(50);
  53. $cod_cab->setSize(50);
  54. $questoes_enunciado->setSize(625,100);
  55. $cod_prova->setEditable(false);
  56. $questoes_enunciado->setEditable(false);
  57. $cabecalho_descricao->setEditable(false);
  58. $descricao->addValidation('Descricao', new TRequiredValidator);
  59. $cod_cab->addValidation('Cabecalho', new TRequiredValidator);
  60. // prova
  61. $table_general->addRowSet( new TLabel('Cód. Prova'), $cod_prova);
  62. $table_general->addRowSet( $label_descricao = new TLabel('Descrição (*)'), $descricao );
  63. $table_general->addRowSet( $label_cabecalho = new TLabel('Cabeçalho (*)'), array( $cod_cab, $cabecalho_descricao ) );
  64. $table_general->addRowSet( new TLabel('Observação'), $observacao );
  65. $label_descricao->setFontColor('#FF0000');
  66. $label_cabecalho->setFontColor('#FF0000');
  67. // questoes
  68. $frame_quest = new TFrame();
  69. $frame_quest->setLegend('Questões');
  70. $row = $tableQuestao->addRow();
  71. $row->addCell($frame_quest);
  72. $add_quest = new TButton('add_quest');
  73. $action_quest = new TAction(array($this, 'onQuestAdd'));
  74. $add_quest->setAction($action_quest, 'Gravar');
  75. $add_quest->setImage('fa:save');
  76. $subtable_quest = new TTable;
  77. $frame_quest->add($subtable_quest);
  78. $subtable_quest->addRowSet( $label_quest = new TLabel('Questão (*)'), array($cod_quest,$questoes_enunciado) );
  79. $subtable_quest->addRowSet( $label_valor = new TLabel('Valor (*)'), $valor );
  80. $subtable_quest->addRowSet( $add_quest );
  81. $label_quest->setFontColor('#FF0000');
  82. $label_valor->setFontColor('#FF0000');
  83. $this->quest_list = new TQuickGrid;
  84. $this->quest_list->setHeight( 175 );
  85. $this->quest_list->makeScrollable();
  86. $this->quest_list->disableDefaultClick();
  87. $this->quest_list->addQuickColumn('', 'edit', 'left', 50);
  88. $this->quest_list->addQuickColumn('', 'delete', 'left', 50);
  89. $this->quest_list->addQuickColumn('Cód. Questão', 'cod_quest', 'left', 50);
  90. $this->quest_list->addQuickColumn('Enunciado', 'questoes_enunciado', 'left', 400);
  91. $this->quest_list->addQuickColumn('Valor','valor', 'left', 80);
  92. $this->quest_list->createModel();
  93. $row = $tableQuestao->addRow();
  94. $row->addCell($this->quest_list);
  95. // create an action button (save)
  96. $save_button=new TButton('save');
  97. $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
  98. $save_button->setImage('ico_save.png');
  99. // create an new button (edit with no parameters)
  100. $new_button=new TButton('new');
  101. $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
  102. $new_button->setImage('ico_new.png');
  103. // define form fields
  104. $this->formFields = array($cod_prova,$descricao, $cod_cab, $cabecalho_descricao, $observacao, $cod_quest, $questoes_enunciado, $valor, $add_quest, $save_button, $new_button);
  105. $this->form->setFields( $this->formFields );
  106. $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
  107. // create the page container
  108. $container = new TVBox;
  109. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  110. $container->add($this->form);
  111. parent::add($container);
  112. }
  113. /**
  114. * Clear form
  115. * @param $param URL parameters
  116. */
  117. function onClear($param)
  118. {
  119. $this->form->clear();
  120. TSession::setValue('quest_prova', array());
  121. $this->onReload( $param );
  122. }
  123. /**
  124. * Add a question into item list
  125. * @param $param URL parameters
  126. */
  127. public function onQuestAdd( $param )
  128. {
  129. try
  130. {
  131. TTransaction::open('super_prova');
  132. $data = $this->form->getData();
  133. if( (! $data->cod_quest) || (! $data->valor) )
  134. throw new Exception('Os campos Questão e Valor são obrigatórios');
  135. $questao = new Questoes($data->cod_quest);
  136. $quest_prova = TSession::getValue('quest_prova');
  137. $key = (int) $data->cod_quest;
  138. $quest_prova[ $key ] = array('cod_quest' => $data->cod_quest,
  139. 'questoes_enunciado' => $data->questoes_enunciado,
  140. 'valor' => $data->valor);
  141. TSession::setValue('quest_prova', $quest_prova);
  142. // clear questao form fields after add
  143. $data->cod_quest = '';
  144. $data->questoes_enunciado = '';
  145. $data->valor = '';
  146. TTransaction::close();
  147. $this->form->setData($data);
  148. $this->onReload( $param ); // reload the sale items
  149. }
  150. catch (Exception $e)
  151. {
  152. $this->form->setData( $this->form->getData());
  153. new TMessage('error', $e->getMessage());
  154. }
  155. }
  156. /**
  157. * Edit a question from item list
  158. * @param $param URL parameters
  159. */
  160. public function onEditItemQuest( $param )
  161. {
  162. $data = $this->form->getData();
  163. // read session items
  164. $quest_prova = TSession::getValue('quest_prova');
  165. // get the session item
  166. $quest_item = $quest_prova[ (int) $param['list_cod_quest'] ];
  167. $data->cod_quest = $param['list_cod_quest'];
  168. $data->questoes_enunciado = $quest_item['questoes_enunciado'];
  169. $data->valor = $quest_item['valor'];
  170. // fill product fields
  171. $this->form->setData( $data );
  172. $this->onReload( $param );
  173. }
  174. /**
  175. * Delete a product from item list
  176. * @param $param URL parameters
  177. */
  178. public function onDeleteItem( $param )
  179. {
  180. $data = $this->form->getData();
  181. $data->cod_quest = '';
  182. $data->questoes_enunciado = '';
  183. $data->valor = '';
  184. // clear form data
  185. $this->form->setData( $data );
  186. // read session items
  187. $quest_prova = TSession::getValue('quest_prova');
  188. // delete the item from session
  189. unset($quest_prova[ (int) $param['list_cod_quest'] ] );
  190. TSession::setValue('quest_prova', $quest_prova);
  191. // reload sale items
  192. $this->onReload( $param );
  193. }
  194. /**
  195. * Reload the item list
  196. * @param $param URL parameters
  197. */
  198. public function onReload($param)
  199. {
  200. // read session items
  201. $quest_prova = TSession::getValue('quest_prova');
  202. $this->quest_list->clear(); // clear product list
  203. $data = $this->form->getData();
  204. if ($quest_prova)
  205. {
  206. $cont = 1;
  207. foreach ($quest_prova as $list_cod_quest => $list_quest)
  208. {
  209. $item_name = 'quest_' . $cont++;
  210. $item = new StdClass;
  211. // create action buttons
  212. $action_del = new TAction(array($this, 'onDeleteItem'));
  213. $action_del->setParameter('list_cod_quest', $list_cod_quest);
  214. $action_edi = new TAction(array($this, 'onEditItemQuest'));
  215. $action_edi->setParameter('list_cod_quest', $list_cod_quest);
  216. $button_del = new TButton('delete_quest'.$cont);
  217. $button_del->class = 'btn btn-default btn-sm';
  218. $button_del->setAction( $action_del, '' );
  219. $button_del->setImage('fa:trash-o red fa-lg');
  220. $button_edi = new TButton('edit_quest'.$cont);
  221. $button_edi->class = 'btn btn-default btn-sm';
  222. $button_edi->setAction( $action_edi, '' );
  223. $button_edi->setImage('fa:edit blue fa-lg');
  224. $item->edit = $button_edi;
  225. $item->delete = $button_del;
  226. $this->formFields[ $item_name.'_edit' ] = $item->edit;
  227. $this->formFields[ $item_name.'_delete' ] = $item->delete;
  228. $item->cod_quest = $list_quest['cod_quest'];
  229. $item->questoes_enunciado = $list_quest['questoes_enunciado'];
  230. $item->valor = $list_quest['valor'];
  231. $row = $this->quest_list->addItem( $item );
  232. $row->onmouseover='';
  233. $row->onmouseout='';
  234. }
  235. $this->form->setFields( $this->formFields );
  236. }
  237. $this->loaded = TRUE;
  238. }
  239. /**
  240. * method onEdit()
  241. * Executed whenever the user clicks at the edit button da datagrid
  242. */
  243. function onEdit($param)
  244. {
  245. try
  246. {
  247. TTransaction::open('super_prova');
  248. if (isset($param['key']))
  249. {
  250. $key = $param['key'];
  251. $object = new Prova($key);
  252. $quest_prova = $object->getQuestProva();
  253. $session_items = array();
  254. foreach( $quest_prova as $item )
  255. {
  256. $session_items[$item->cod_quest] = $item->toArray();
  257. $session_items[$item->cod_quest]['cod_quest'] = $item->cod_quest;
  258. $session_items[$item->cod_quest]['questoes_enunciado'] = $item->questoes_enunciado;
  259. $session_items[$item->cod_quest]['valor'] = $item->valor;
  260. }
  261. TSession::setValue('quest_prova', $session_items);
  262. $this->form->setData($object); // fill the form with the active record data
  263. $this->onReload( $param ); // reload sale items list
  264. TTransaction::close(); // close transaction
  265. }
  266. else
  267. {
  268. $this->form->clear();
  269. TSession::setValue('quest_prova', null);
  270. $this->onReload( $param );
  271. }
  272. }
  273. catch (Exception $e) // in case of exception
  274. {
  275. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  276. TTransaction::rollback();
  277. }
  278. }
  279. /**
  280. * Save the sale and the sale items
  281. */
  282. function onSave()
  283. {
  284. try
  285. {
  286. // open a transaction with database 'super_prova'
  287. TTransaction::open('super_prova');
  288. $Prova = $this->form->getData('Prova');
  289. $this->form->validate(); // form validation
  290. // get session questoes
  291. $quest_prova = TSession::getValue('quest_prova');
  292. if( $quest_prova )
  293. {
  294. foreach( $quest_prova as $quest_item )
  295. {
  296. $item = new QuestProva;
  297. $item->cod_quest = $quest_item['cod_quest'];
  298. $item->valor = $quest_item['valor'];
  299. $Prova->addQuestProva($item);
  300. }
  301. }
  302. $Prova->store(); // stores the object
  303. $this->form->setData($Prova); // keep form data
  304. TTransaction::close(); // close the transaction
  305. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  306. }
  307. catch (Exception $e) // in case of exception
  308. {
  309. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  310. $this->form->setData( $this->form->getData() ); // keep form data
  311. TTransaction::rollback();
  312. }
  313. }
  314. /**
  315. * Show the page
  316. */
  317. public function show()
  318. {
  319. // check if the datagrid is already loaded
  320. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  321. {
  322. $this->onReload( func_get_arg(0) );
  323. }
  324. parent::show();
  325. }
  326. }
  327. ?>


?>
NR

Habilita os logs do sql logo após o TTransaction::open na onSave:
 
  1. <?php
  2. TTransaction::setLogger(new TLoggerSTD());
  3. ?>
LB

Já habilitei onde o arquivo de log é gerado?

LB

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.
 
  1. <?php e
 
  1. <?php
  2. function onSave()
  3. {
  4. try
  5. {
  6. // open a transaction with database 'super_prova'
  7. TTransaction::open('super_prova');
  8. TTransaction::setLogger(new TLoggerSTD());
  9. $Prova = $this->form->getData('Prova');
  10. $this->form->validate(); // form validation
  11. // get session questoes
  12. $Prova->store(); // stores the object
  13. $quest_prova = TSession::getValue('quest_prova');
  14. if( $quest_prova )
  15. {
  16. foreach( $quest_prova as $quest_item )
  17. {
  18. $item = new QuestProva;
  19. $item->cod_quest = $quest_item['cod_quest'];
  20. $item->valor = $quest_item['valor'];
  21. $item->cod_prova = $Prova->cod_prova;
  22. $Prova->addQuestProva($item);
  23. $item->store();
  24. }
  25. }
  26. ?>

?>