Erro ao Gravar registro Mestre-Detalhe com SQL Server Conectei ao SQL Server 2008 e montei um form Master/Detail para inserir alguns registros. Me baseei em uma outra tela Master/Detail que eu já tinha construido conectando ao MySQL. Ao tentar gravar o registro no form, ocorre o seguinte erro: SQLSTATE[22018]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value '3::Farinha Branca Nico' to ...
MC
Erro ao Gravar registro Mestre-Detalhe com SQL Server  
Conectei ao SQL Server 2008 e montei um form Master/Detail para inserir alguns registros.
Me baseei em uma outra tela Master/Detail que eu já tinha construido conectando ao MySQL.

Ao tentar gravar o registro no form, ocorre o seguinte erro:

SQLSTATE[22018]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value '3::Farinha Branca Nico' to data type int.

O mesmo form, alterando a string de conexão e apontando para o MySQL, com exatamente a mesma estrutura de campos e tipos, funciona.
Porém ao apontar a conexão para o SQL Server ocorre o erro acima.

Já li o artigo do Regis falando sobre SQL Server X Adianti. Segui tudo que estava ali e funcionou a conexão e outros aspectos, porém esse detalhe tem me dado dor de cabeça.

Não sei se posso estar fazendo algo errado, alguém pode me ajudar?

Segue abaixo o código do meu form:

 
  1. <?php
 
  1. <?php
  2. /**
  3. * PesquisaCabForm Master/Detail
  4. * @author <your name here>
  5. */
  6. class PesquisaCabForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $table_details;
  10. protected $detail_row;
  11. /**
  12. * Class constructor
  13. * Creates the page and the registration form
  14. */
  15. function __construct($param)
  16. {
  17. parent::__construct($param);
  18. // creates the form
  19. $this->form = new TForm('form_PesquisaCab');
  20. $this->form->class = 'tform'; // CSS class
  21. $table_master = new TTable;
  22. $table_master->width = '100%';
  23. $table_master->addRowSet( new TLabel('PesquisaCab'), '', '')->class = 'tformtitle';
  24. // add a table inside form
  25. $table_general = new TTable;
  26. $table_general->width = '100%';
  27. $frame_general = new TFrame;
  28. $frame_general->class = 'tframe tframe-custom';
  29. $frame_general->setLegend('PesquisaCab');
  30. $frame_general->style = 'background:whiteSmoke';
  31. $frame_general->add($table_general);
  32. $frame_details = new TFrame;
  33. $frame_details->class = 'tframe tframe-custom';
  34. $frame_details->setLegend('PesquisaItem');
  35. $table_master->addRow()->addCell( $frame_general )->colspan=2;
  36. $row = $table_master->addRow();
  37. $row->addCell( $frame_details );
  38. $this->form->add($table_master);
  39. // master fields
  40. $id = new TEntry('id');
  41. $promotor_id = new ">TDBSeekButton('promotor_id', 'promotor', 'form_PesquisaCab', 'promotor', 'Nome', 'promotor_id', 'promotorNome');
  42. $promotorNome = new TEntry('promotorNome');
  43. $cliente_id = new ">TDBSeekButton('cliente_id', 'promotor', 'form_PesquisaCab', 'cliente', 'Nome', 'cliente_id', 'clienteNome');
  44. $clienteNome = new TEntry('clienteNome');
  45. $dataPesq = new TDate('dataPesq');
  46. $horaPesq = new TEntry('horaPesq');
  47. $obsPesq = new TText('obsPesq');
  48. // sizes
  49. $id->setSize('100');
  50. $promotor_id->setSize('100');
  51. $promotorNome->setSize('300');
  52. $promotorNome->setEditable(FALSE);
  53. $cliente_id->setSize('100');
  54. $clienteNome->setSize('300');
  55. $clienteNome->setEditable(FALSE);
  56. $dataPesq->setSize('100');
  57. $dataPesq->setEditable(FALSE);
  58. $horaPesq->setSize('200');
  59. $horaPesq->setEditable(FALSE);
  60. $obsPesq->setSize('200');
  61. if (!empty($id))
  62. {
  63. $id->setEditable(FALSE);
  64. }
  65. // add form fields to be handled by form
  66. $this->form->addField($id);
  67. $this->form->addField($promotor_id);
  68. $this->form->addField($cliente_id);
  69. $this->form->addField($dataPesq);
  70. $this->form->addField($horaPesq);
  71. $this->form->addField($obsPesq);
  72. // add form fields to the screen
  73. $table_general->addRowSet( new TLabel('Id'), $id );
  74. $table_general->addRowSet( $label_promotor = new TLabel('Promotor'), array( $promotor_id, $promotorNome ) );
  75. $table_general->addRowSet( $label_cliente = new TLabel('Cliente'), array( $cliente_id, $clienteNome ) );
  76. $table_general->addRowSet( new TLabel('Data'), $dataPesq );
  77. $table_general->addRowSet( new TLabel('Hora'), $horaPesq );
  78. $table_general->addRowSet( new TLabel('Observações'), $obsPesq );
  79. // detail
  80. $this->table_details = new TTable;
  81. $this->table_details-> width = '100%';
  82. $frame_details->add($this->table_details);
  83. $this->table_details->addSection('thead');
  84. $row = $this->table_details->addRow();
  85. // detail header
  86. $row->addCell( new TLabel('Produto Equivalente') );
  87. $row->addCell( new TLabel('Descrição') );
  88. $row->addCell( new TLabel('Marca') );
  89. $row->addCell( new TLabel('Preço') );
  90. // create an action button (save)
  91. $save_button=new TButton('save');
  92. $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
  93. $save_button->setImage('ico_save.png');
  94. // create an new button (edit with no parameters)
  95. $new_button=new TButton('new');
  96. $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
  97. $new_button->setImage('ico_new.png');
  98. // define form fields
  99. $this->form->addField($save_button);
  100. $this->form->addField($new_button);
  101. $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
  102. $this->detail_row = 0;
  103. // create the page container
  104. $container = new TVBox;
  105. $container->style = 'width: 100%';
  106. //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  107. $container->add($this->form);
  108. parent::add($container);
  109. }
  110. /**
  111. * Executed whenever the user clicks at the edit button da datagrid
  112. */
  113. function onEdit($param)
  114. {
  115. try
  116. {
  117. TTransaction::open('promotor');
  118. if (isset($param['key']))
  119. {
  120. $key = $param['key'];
  121. $object = new PesquisaCab($key);
  122. $this->form->setData($object);
  123. $items = PesquisaItem::where('pesquisaCab_id', '=', $key)->load();
  124. $this->table_details->addSection('tbody');
  125. if ($items)
  126. {
  127. foreach($items as $item )
  128. {
  129. $this->addDetailRow($item);
  130. }
  131. // create add button
  132. $add = new TButton('clone');
  133. $add->setLabel('Add');
  134. $add->setImage('fa:plus-circle green');
  135. $add->addFunction('ttable_clone_previous_row(this)');
  136. // add buttons in table
  137. $this->table_details->addRowSet([$add]);
  138. }
  139. else
  140. {
  141. $this->onClear($param);
  142. }
  143. TTransaction::close(); // close transaction
  144. }
  145. }
  146. catch (Exception $e) // in case of exception
  147. {
  148. new TMessage('error', $e->getMessage());
  149. TTransaction::rollback();
  150. }
  151. try
  152. {
  153. if (isset($param['id']))
  154. {
  155. // open a transaction with database 'mvadmin'
  156. TTransaction::open('promotor');
  157. // get the parameter $key
  158. $key = $param['id'];
  159. // instantiates object Promotor
  160. $object = new PesquisaCab($key);
  161. $object->promotorNome = $object->promotor->Nome;
  162. $object->clienteNome = $object->cliente->Nome;
  163. // fill the form with the active record data
  164. $this->form->setData($object);
  165. // close the transaction
  166. TTransaction::close();
  167. }
  168. else
  169. {
  170. $this->form->clear();
  171. }
  172. }
  173. catch (Exception $e) // in case of exception
  174. {
  175. // shows the exception error message
  176. new TMessage('error', 'Error ' . $e->getMessage());
  177. // undo all pending operations
  178. TTransaction::rollback();
  179. }
  180. }
  181. /**
  182. * Add detail row
  183. */
  184. public function addDetailRow($item)
  185. {
  186. $uniqid = mt_rand(1000000, 9999999);
  187. // create fields
  188. // $produto_id = new TEntry('produto_id[]');
  189. $produto_id = new TDBMultiSearch('produto_id[]', 'promotor', 'produto', 'id', 'descricao');
  190. $descItem = new TEntry('descItem[]');
  191. $marcaItem = new TEntry('marcaItem[]');
  192. $valorItem = new TEntry('valorItem[]');
  193. // set id's
  194. $produto_id->setId('produto_id_'.$uniqid);
  195. $descItem->setId('descItem_'.$uniqid);
  196. $marcaItem->setId('marcaItem_'.$uniqid);
  197. $valorItem->setId('valorItem_'.$uniqid);
  198. // set sizes
  199. $produto_id->setSize('200');
  200. $descItem->setSize('200');
  201. $marcaItem->setSize('200');
  202. $valorItem->setSize('200');
  203. $produto_id->setMaxSize(1);
  204. $produto_id->setMinLength(1);
  205. // set row counter
  206. $produto_id->{'data-row'} = $this->detail_row;
  207. $descItem->{'data-row'} = $this->detail_row;
  208. $marcaItem->{'data-row'} = $this->detail_row;
  209. $valorItem->{'data-row'} = $this->detail_row;
  210. // set value
  211. // if (!empty($item->produto_id)) { $produto_id->setValue( $item->produto_id ); }
  212. if (!empty($item->produto_id)) { $produto_id->setValue( [$item->produto_id => $item->produto->descricao . " ({$item->produto_id})"] ); }
  213. if (!empty($item->descItem)) { $descItem->setValue( $item->descItem ); }
  214. if (!empty($item->marcaItem)) { $marcaItem->setValue( $item->marcaItem ); }
  215. if (!empty($item->valorItem)) { $valorItem->setValue( $item->valorItem ); }
  216. // create delete button
  217. $del = new TImage('fa:trash-o red');
  218. $del->onclick = 'ttable_remove_row(this)';
  219. $row = $this->table_details->addRow();
  220. // add cells
  221. $row->addCell($produto_id);
  222. $row->addCell($descItem);
  223. $row->addCell($marcaItem);
  224. $row->addCell($valorItem);
  225. $row->addCell( $del );
  226. $row->{'data-row'} = $this->detail_row;
  227. // add form field
  228. $this->form->addField($produto_id);
  229. $this->form->addField($descItem);
  230. $this->form->addField($marcaItem);
  231. $this->form->addField($valorItem);
  232. $this->detail_row ++;
  233. }
  234. /**
  235. * Clear form
  236. */
  237. public function onClear($param)
  238. {
  239. $this->table_details->addSection('tbody');
  240. $this->addDetailRow( new stdClass );
  241. // create add button
  242. $add = new TButton('clone');
  243. $add->setLabel('Add');
  244. $add->setImage('fa:plus-circle green');
  245. $add->addFunction('ttable_clone_previous_row(this)');
  246. // add buttons in table
  247. $this->table_details->addRowSet([$add]);
  248. }
  249. /**
  250. * Save the PesquisaCab and the PesquisaItem's
  251. */
  252. public static function onSave($param)
  253. {
  254. try
  255. {
  256. TTransaction::open('promotor');
  257. $id = (int) $param['id'];
  258. $master = new PesquisaCab;
  259. $master->fromArray( $param);
  260. $master->store(); // save master object
  261. // delete details
  262. PesquisaItem::where('pesquisaCab_id', '=', $master->id)->delete();
  263. if( !empty($param['produto_id']) AND is_array($param['produto_id']) )
  264. {
  265. foreach( $param['produto_id'] as $row => $produto_id)
  266. {
  267. if (!empty($produto_id))
  268. {
  269. $detail = new PesquisaItem;
  270. $detail->pesquisaCab_id = $master->id;
  271. $detail->produto_id = $param['produto_id'][$row];
  272. $detail->descItem = $param['descItem'][$row];
  273. $detail->marcaItem = $param['marcaItem'][$row];
  274. $detail->valorItem = $param['valorItem'][$row];
  275. $detail->store();
  276. }
  277. }
  278. }
  279. $data = new stdClass;
  280. $data->id = $master->id;
  281. TForm::sendData('form_PesquisaCab', $data);
  282. TTransaction::close(); // close the transaction
  283. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  284. }
  285. catch (Exception $e) // in case of exception
  286. {
  287. new TMessage('error', $e->getMessage());
  288. TTransaction::rollback();
  289. }
  290. }
  291. }
  292. ?>

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 (4)


NR

Marcelo, o erro diz que você está tentando salvar um varchar em um campo int. O Mysql deve converter automaticamente e por isso não apresenta o erro. Como a função onSave é static alguns tratamentos dos campos não são feitos automaticamente. Você precisa extrair somente o id do produto antes de salvar cada detalhe:
 
  1. <?php
  2. list($id_produto,$desc_produto) = explode('::', $param['produto_id'][$row]);
  3. $detail->produto_id = $id_produto;
  4. ?>
MC

Obrigado Nataniel. Funcionou perfeitamente.
NC

Olá, estava com este mesmo problema.aApós criar o formulário mestre/detalhe com o Studio, resolvi utilizando as informações que o Nataniel informou.
Entretanto só consigo adicionar até o terceiro registro, aparece a mensagem que o "registro foi salvo ".
Mas no banco de dados da tabela referente ao registro de detalhe, todos os campos ficam gravados com NULL.

Obs.: Um detalhe interessante, se eu for na base de dados e incluir manualmente algum registro, por ex: "nome", ao voltar para a datagrid referente e selecionar o registro com os campos null, consigo salvar no banco de dados os outros registros através do formulário.
FT

Também estou no sql server e estou tendo dificuldade em gravar o id do mestre no detalhe. O id do mestre, que normalmente fica disponível após o $master->store(); simplesmente não grava no detalhe, apesar de gravar o mestre corretamente no banco de dados. Mesmo na página gerada pelo studio está dando este problema.