Conheça as melhorias da versão 8.0, 8.1, 8.2!
Clique aqui para saber mais
TMultiFile problema na Edição ! etsou uasndo o TMultiFile para salvar imagem do produto. Na inclusao ele funciona. Quando abro a tela para Edição ele mostra a imagem. Mas quando vou salvar a edicao da erro : Segue as classes utilziadas e o formulario. ERRO : SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "files/images/1/icon.png" ---------------------------------------------...
PG
TMultiFile problema na Edição !  
etsou uasndo o TMultiFile para salvar imagem do produto.
Na inclusao ele funciona.
Quando abro a tela para Edição ele mostra a imagem.

Mas quando vou salvar a edicao da erro :
Segue as classes utilziadas e o formulario.
ERRO : SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "files/images/1/icon.png"

------------------------------------------------------------------------------------------------------------------
 
  1. <?php
  2. /**
  3. * Produto Active Record
  4. * @author <your-name-here>
  5. */
  6. class Produto extends TRecord
  7. {
  8. const TABLENAME = '"public"."produto"';
  9. const PRIMARYKEY= 'id';
  10. const IDPOLICY = 'max'; // {max, serial}
  11. const CREATEDAT = 'created_at';
  12. const UPDATEDAT = 'updated_at';
  13. /**
  14. * Constructor method
  15. */
  16. public function __construct($id = NULL, $callObjectLoad = TRUE)
  17. {
  18. parent::__construct($id, $callObjectLoad);
  19. parent::addAttribute('usuarioinclusao_id');
  20. parent::addAttribute('usuarioalteracao_id');
  21. parent::addAttribute('unidade_id');
  22. parent::addAttribute('sdescricao');
  23. parent::addAttribute('sdescricaonf');
  24. parent::addAttribute('sreferencia');
  25. parent::addAttribute('created_at');
  26. parent::addAttribute('updated_at');
  27. parent::addAttribute('marca_id');
  28. parent::addAttribute('grupoproduto_id');
  29. parent::addAttribute('npesoliquido');
  30. parent::addAttribute('npesobruto');
  31. parent::addAttribute('nprecocusto');
  32. parent::addAttribute('nprecovenda');
  33. parent::addAttribute('nporcentagemlucrodesejado');
  34. parent::addAttribute('nlucrodesejado');
  35. parent::addAttribute('nprecosugerido');
  36. parent::addAttribute('fornecedor_id');
  37. parent::addAttribute('stipoproduto');
  38. parent::addAttribute('nprecopromocao');
  39. parent::addAttribute('sativapromocao');
  40. parent::addAttribute('nestoqueminimo');
  41. parent::addAttribute('nestoquemaximo');
  42. parent::addAttribute('nestoquesaldo');
  43. parent::addAttribute('sespecificacao');
  44. parent::addAttribute('scodigogtin');
  45. parent::addAttribute('scodigoanp');
  46. parent::addAttribute('sippt');
  47. parent::addAttribute('siat');
  48. parent::addAttribute('slocalizacaocorredor');
  49. parent::addAttribute('slocalizacaoposicaohorizontal');
  50. parent::addAttribute('slocalizacaoposicaovertical');
  51. parent::addAttribute('ncm_id');
  52. parent::addAttribute('scodigogtintrib');
  53. parent::addAttribute('dt_ultimacompra');
  54. }
  55. public function get_unidade()
  56. {
  57. return Unidade::find($this->unidade_id);
  58. }
  59. public function get_marca()
  60. {
  61. return Marca::find($this->marca_id);
  62. }
  63. public function get_grupoproduto()
  64. {
  65. return Grupoproduto::find($this->grupoproduto_id);
  66. }
  67. public function get_fornecedor()
  68. {
  69. return Pessoa::find($this->fornecedor_id);
  70. }
  71. public function get_ncm()
  72. {
  73. return Ncm::where(scodigoncm,'=',$this->ncm_id)->load()->first();
  74. }
  75. }
 
  1. <?php
  2. /**
  3. * ProdutoImagem Active Record
  4. * @author <your-name-here>
  5. */
  6. class ProdutoImagem extends TRecord
  7. {
  8. const TABLENAME = '"public"."produto_imagem"';
  9. const PRIMARYKEY= 'id';
  10. const IDPOLICY = 'max'; // {max, serial}
  11. const CREATEDAT = 'created_at';
  12. const UPDATEDAT = 'updated_at';
  13. /**
  14. * Constructor method
  15. */
  16. public function __construct($id = NULL, $callObjectLoad = TRUE)
  17. {
  18. parent::__construct($id, $callObjectLoad);
  19. parent::addAttribute('produto_id');
  20. parent::addAttribute('spath');
  21. parent::addAttribute('created_at');
  22. parent::addAttribute('updated_at');
  23. }
  24. }
 
  1. <?php
  2. /**
  3. * ProdutoForm Form
  4. * @author <your name here>
  5. */
  6. class ProdutoForm extends TWindow
  7. {
  8. protected $form; // form
  9. use Adianti\Base\AdiantiFileSaveTrait;
  10. /**
  11. * Form constructor
  12. * @param $param Request
  13. */
  14. public function __construct( $param )
  15. {
  16. parent::__construct();
  17. parent::include_js('app/lib/include/application.js');
  18. parent::setSize(0.8, 0.80);
  19. parent::removePadding();
  20. parent::removeTitleBar();
  21. // creates the form
  22. $this->form = new BootstrapFormBuilder('form_Produto');
  23. $this->form->setFormTitle('Produto');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $usuarioinclusao_id = new TEntry('usuarioinclusao_id');
  27. $usuarioalteracao_id = new TEntry('usuarioalteracao_id');
  28. $unidade_id = new TDBUniqueSearch('unidade_id', 'tutorerpdfe', 'Unidade', 'id', 'sdescricao');
  29. $sdescricao = new TEntry('sdescricao');
  30. $sdescricaonf = new TEntry('sdescricaonf');
  31. $sreferencia = new TEntry('sreferencia');
  32. $marca_id = new TDBUniqueSearch('marca_id', 'tutorerpdfe', 'Marca', 'id', 'snome');
  33. $grupoproduto_id = new TDBUniqueSearch('grupoproduto_id', 'tutorerpdfe', 'Grupoproduto', 'id', 'snome');
  34. $npesoliquido = new TEntry('npesoliquido');
  35. $npesobruto = new TEntry('npesobruto');
  36. $nprecocusto = new TEntry('nprecocusto');
  37. $nprecovenda = new TEntry('nprecovenda');
  38. $nporcentagemlucrodesejado = new TEntry('nporcentagemlucrodesejado');
  39. $nlucrodesejado = new TEntry('nlucrodesejado');
  40. $nprecosugerido = new TEntry('nprecosugerido');
  41. $fornecedor_id = new TDBUniqueSearch('fornecedor_id', 'tutorerpdfe', 'Pessoa', 'id', 'nome');
  42. $stipoproduto = new TCombo('stipoproduto');
  43. $nprecopromocao = new TEntry('nprecopromocao');
  44. $sativapromocao = new TCombo('sativapromocao');
  45. $nestoqueminimo = new TEntry('nestoqueminimo');
  46. $nestoquemaximo = new TEntry('nestoquemaximo');
  47. $nestoquesaldo = new TEntry('nestoquesaldo');
  48. $sespecificacao = new TText('sespecificacao');
  49. $scodigogtin = new TEntry('scodigogtin');
  50. $scodigoanp = new TEntry('scodigoanp');
  51. $sippt = new TCombo('sippt');
  52. $siat = new TCombo('siat');
  53. $slocalizacaocorredor = new TEntry('slocalizacaocorredor');
  54. $slocalizacaoposicaohorizontal = new TEntry('slocalizacaoposicaohorizontal');
  55. $slocalizacaoposicaovertical = new TEntry('slocalizacaoposicaovertical');
  56. $ncm_id = new TDBUniqueSearch('ncm_id', 'tutorerpdfe', 'Ncm', 'scodigoncm', 'scodigoncm');
  57. $scodigogtintrib = new TEntry('scodigogtintrib');
  58. $dt_ultimacompra = new TDate('dt_ultimacompra');
  59. $spath = new TMultiFile('spath');
  60. $this->form->addField( $usuarioinclusao_id );
  61. $this->form->addField( $usuarioalteracao_id );
  62. // add the fields
  63. $row=$this->form->addFields( [ new TLabel('Id') ], [ $id ] );
  64. $row->class = 'form-control-sm';
  65. $row=$this->form->addFields( [ new TLabel('Descricao','red') ], [ $sdescricao ] , [ new TLabel('Descricao NFe','red') ], [ $sdescricaonf ] );
  66. $row->class = 'form-control-sm';
  67. $row=$this->form->addFields( [ new TLabel('Referencia','red') ], [ $sreferencia ] , [ new TLabel('Unidade','red') ], [ $unidade_id ] );
  68. $row->class = 'form-control-sm';
  69. $row=$this->form->addFields( [ new TLabel('Marca','red') ], [ $marca_id ] , [ new TLabel('Grupo Produto','red') ], [ $grupoproduto_id ] );
  70. $row->class = 'form-control-sm';
  71. $row=$this->form->addFields( [ new TLabel('R$ Custo') ], [ $nprecocusto ] , [ new TLabel('R$ Venda') ], [ $nprecovenda ]);
  72. $row->class = 'form-control-sm';
  73. $row=$this->form->addFields( [ new TLabel('Peso Liq.') ], [ $npesoliquido ] , [ new TLabel('Peso Br.') ], [ $npesobruto ]
  74. );
  75. $row->class = 'form-control-sm';
  76. $row=$this->form->addFields( [ new TLabel('% Lucro Desejado') ], [ $nporcentagemlucrodesejado ] ,
  77. [ new TLabel('Lucro Desejado') ], [ $nlucrodesejado ] ,
  78. [ new TLabel('Preco Sugerido') ], [ $nprecosugerido ] );
  79. $row->class = 'form-control-sm';
  80. $row=$this->form->addFields( [ new TLabel('Fornecedor','red') ], [ $fornecedor_id ] ,[ new TLabel('Dt Ultima Compra') ], [ $dt_ultimacompra ] );
  81. $row->class = 'form-control-sm';
  82. $row=$this->form->addFields( [ new TLabel('Tipo Produto','red') ], [ $stipoproduto ] ,
  83. [ new TLabel('Preco Promoção') ], [ $nprecopromocao ] ,
  84. [ new TLabel('Promoção Ativa') ], [ $sativapromocao ] );
  85. $row->layout = [ 'col-sm-2 control-label', 'col-sm-3',
  86. 'col-sm-2 control-label', 'col-sm-2',
  87. 'col-sm-2 control-label', 'col-sm-1' ];
  88. $row->class = 'form-control-sm';
  89. $row=$this->form->addFields( [ new TLabel('Estoque Minimo') ], [ $nestoqueminimo ] ,
  90. [ new TLabel('Estoque Maximo') ], [ $nestoquemaximo ] ,
  91. [ new TLabel('Estoque Saldo') ], [ $nestoquesaldo ] );
  92. $row->class = 'form-control-sm';
  93. $row=$this->form->addFields(
  94. [ new TLabel('Codigo ANP') ], [ $scodigoanp ] ,
  95. [ new TLabel('SIAT') ], [ $siat ], [ new TLabel('IPPT') ], [ $sippt ] ,[new TLabel('Imagem')], [$spath] );
  96. $row->layout = [ 'col-sm-2 control-label', 'col-sm-1',
  97. 'col-sm-2 control-label', 'col-sm-2',
  98. 'col-sm-1 control-label', 'col-sm-1',
  99. 'col-sm-1 control-label', 'col-sm-2' ];
  100. $row->class = 'form-control-sm';
  101. $row=$this->form->addFields( [ new TLabel('Localização Corredor') ], [ $slocalizacaocorredor ] ,
  102. [ new TLabel('Hizontal') ], [ $slocalizacaoposicaohorizontal ] ,
  103. [ new TLabel('Vertical') ], [ $slocalizacaoposicaovertical ] ,[]);
  104. $row->layout = [ 'col-sm-2 control-label', 'col-sm-1',
  105. 'col-sm-2 control-label', 'col-sm-2',
  106. 'col-sm-1 control-label', 'col-sm-1',
  107. 'col-sm-1 control-label', 'col-sm-2' ];
  108. $row->class = 'form-control-sm';
  109. $row=$this->form->addFields( [ new TLabel('NCM','red') ], [ $ncm_id ] , [ new TLabel('Codigo GTIN') ], [ $scodigogtin ] , [ new TLabel('GTIN Trib') ], [ $scodigogtintrib ] ,[]);
  110. $row->layout = [ 'col-sm-2 control-label', 'col-sm-2',
  111. 'col-sm-2 control-label', 'col-sm-2',
  112. 'col-sm-1 control-label', 'col-sm-1',
  113. 'col-sm-1 control-label', 'col-sm-1' ];
  114. $row->class = 'form-control-sm';
  115. $row=$this->form->addFields( [ new TLabel('Especificação') ], [ $sespecificacao ],[] );
  116. $row->class = 'form-control-sm';
  117. $row=$this->form->addFields( [ ], [ ],[] );
  118. $row->class = 'form-control-sm';
  119. // set sizes
  120. $id->setSize('100%');
  121. $usuarioinclusao_id->setSize('100%');
  122. $usuarioalteracao_id->setSize('100%');
  123. $unidade_id->setSize('100%');
  124. $sdescricao->setSize('100%');
  125. $sdescricaonf->setSize('100%');
  126. $sreferencia->setSize('100%');
  127. $marca_id->setSize('100%');
  128. $grupoproduto_id->setSize('100%');
  129. $npesoliquido->setSize('100%');
  130. $npesobruto->setSize('100%');
  131. $nprecocusto->setSize('100%');
  132. $nprecovenda->setSize('100%');
  133. $nporcentagemlucrodesejado->setSize('100%');
  134. $nlucrodesejado->setSize('100%');
  135. $nprecosugerido->setSize('100%');
  136. $fornecedor_id->setSize('100%');
  137. $stipoproduto->setSize('100%');
  138. $nprecopromocao->setSize('100%');
  139. $sativapromocao->setSize('100%');
  140. $nestoqueminimo->setSize('100%');
  141. $nestoquemaximo->setSize('100%');
  142. $nestoquesaldo->setSize('100%');
  143. $sespecificacao->setSize('100%');
  144. $scodigogtin->setSize('100%');
  145. $scodigoanp->setSize('100%');
  146. $sippt->setSize('100%');
  147. $siat->setSize('100%');
  148. $slocalizacaocorredor->setSize('100%');
  149. $slocalizacaoposicaohorizontal->setSize('100%');
  150. $slocalizacaoposicaovertical->setSize('100%');
  151. $ncm_id->setSize('100%');
  152. $scodigogtintrib->setSize('100%');
  153. $dt_ultimacompra->setSize('100%');
  154. $sespecificacao->setSize(730,60);
  155. $spath->setAllowedExtensions( ['gif', 'png', 'jpg', 'jpeg'] );
  156. $spath->enableFileHandling();
  157. $spath->enableImageGallery();
  158. $spath->enablePopover('Preview', '<img style="max-width:250px" src="download.php?file={file_name}">');
  159. $sippt->addItems( ['P' => 'PROPRIO', 'T' => 'TERCEIROS'] );
  160. $sippt->setValue('P');
  161. $siat->addItems( ['A' => 'ARREDONDAMENTO', 'T' => 'TRUNCAMENTO'] );
  162. $siat->setValue('A');
  163. $stipoproduto->addItems( ['PR' => 'PROPRIO REVENDA', 'TR' => 'TERCEIROS REVENDA', 'CO' => 'CONSUMO', 'MP' => 'MATÉRIA PRIMA' ] );
  164. $stipoproduto->setValue('TR');
  165. $sativapromocao->addItems( ['N' => 'NÃO', 'S' => 'SIM' ] );
  166. $sativapromocao->setValue('N');
  167. $dt_ultimacompra->setMask('dd/mm/yyyy',true);
  168. $dt_ultimacompra->setDatabaseMask('yyyy-mm-dd');
  169. $dt_ultimacompra->setOption('showOnFocus', false);
  170. $dt_ultimacompra->setOption('triggerEvent', 'dblclick');
  171. $npesoliquido->setNumericMask(4, ',', '.', true);
  172. $npesobruto->setNumericMask(4, ',', '.', true);
  173. $nprecocusto->setNumericMask(4, ',', '.', true);
  174. $nprecovenda->setNumericMask(4, ',', '.', true);
  175. $nporcentagemlucrodesejado->setNumericMask(4, ',', '.', true);
  176. $nlucrodesejado->setNumericMask(4, ',', '.', true);
  177. $nprecosugerido->setNumericMask(4, ',', '.', true);
  178. $nprecopromocao->setNumericMask(4, ',', '.', true);
  179. $nestoqueminimo->setNumericMask(4, ',', '.', true);
  180. $nestoquemaximo->setNumericMask(4, ',', '.', true);
  181. $nestoquesaldo->setNumericMask(4, ',', '.', true);
  182. $npesoliquido->setValue('0');
  183. $npesobruto->setValue('0');
  184. $nprecocusto->setValue('0');
  185. $nprecovenda->setValue('0');
  186. $nporcentagemlucrodesejado->setValue('0');
  187. $nlucrodesejado->setValue('0');
  188. $nprecosugerido->setValue('0');
  189. $nprecopromocao->setValue('0');
  190. $nestoqueminimo->setValue('0');
  191. $nestoquemaximo->setValue('0');
  192. $nestoquesaldo->setValue('0');
  193. $unidade_id->setMinLength(0);
  194. $marca_id->setMinLength(0);
  195. $grupoproduto_id->setMinLength(0);
  196. $sdescricao->forceUpperCase();
  197. $sdescricaonf->forceUpperCase();
  198. $sreferencia->forceUpperCase();
  199. $slocalizacaocorredor->forceUpperCase();
  200. $slocalizacaoposicaohorizontal->forceUpperCase();
  201. $slocalizacaoposicaovertical->forceUpperCase();
  202. if (!empty($id))
  203. {
  204. $id->setEditable(FALSE);
  205. }
  206. /** samples
  207. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  208. $fieldX->setSize( '100%' ); // set size
  209. **/
  210. $this->form->addHeaderActionLink( _t('Close'), new TAction([__CLASS__, 'onClose'], ['static'=>'1']), 'fa:times red');
  211. // create the form actions
  212. $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
  213. $btn->class = 'btn btn-sm btn-primary';
  214. $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
  215. // vertical box container
  216. $container = new TVBox;
  217. $container->style = 'width: 100%';
  218. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  219. $container->add($this->form);
  220. parent::add($container);
  221. }
  222. /**
  223. * Save form data
  224. * @param $param Request
  225. */
  226. public function onSave( $param )
  227. {
  228. try
  229. {
  230. TTransaction::open('tutorerpdfe'); // open a transaction
  231. /**
  232. // Enable Debug logger for SQL operations inside the transaction
  233. TTransaction::setLogger(new TLoggerSTD); // standard output
  234. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  235. **/
  236. $this->form->validate(); // validate form data
  237. $data = $this->form->getData(); // get form data as array
  238. $object = new Produto; // create an empty object
  239. $object->fromArray( (array) $data); // load the object with data
  240. if (!empty($object->id)) {
  241. $objectImagem = new ProdutoImagem;
  242. $objectImagem->delete($object->id);
  243. }
  244. $this->saveFiles($object, $data, 'spath', 'files/images', 'ProdutoImagem', 'spath', 'produto_id');
  245. if (empty($object->id)) {
  246. $object->usuarioinclusao_id = TSession::getValue('userid');
  247. $object->usuarioalteracao_id = '0';
  248. } else {
  249. $object->usuarioalteracao_id = TSession::getValue('userid');
  250. }
  251. if (empty($object->fornecedor_id)) {
  252. $object->fornecedor_id = '0';
  253. }
  254. $object->store(); // save the object
  255. // get the generated id
  256. $data->id = $object->id;
  257. $this->form->setData($data); // fill form data
  258. TTransaction::close(); // close the transaction
  259. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  260. }
  261. catch (Exception $e) // in case of exception
  262. {
  263. new TMessage('error', $e->getMessage()); // shows the exception error message
  264. $this->form->setData( $this->form->getData() ); // keep form data
  265. TTransaction::rollback(); // undo all pending operations
  266. }
  267. }
  268. /**
  269. * Clear form data
  270. * @param $param Request
  271. */
  272. public function onClear( $param )
  273. {
  274. $this->form->clear(TRUE);
  275. }
  276. /**
  277. * Load object to form data
  278. * @param $param Request
  279. */
  280. public function onEdit( $param )
  281. {
  282. try
  283. {
  284. if (isset($param['key']))
  285. {
  286. $key = $param['key']; // get the parameter $key
  287. TTransaction::open('tutorerpdfe'); // open a transaction
  288. $object = new Produto($key); // instantiates the Active Record
  289. $this->form->setData($object); // fill the form
  290. $data = new stdClass;
  291. $data->spath = ProdutoImagem::where('produto_id', '=', $param['key'])->getIndexedArray('spath');
  292. $this->form->setData($data);
  293. TTransaction::close(); // close the transaction
  294. }
  295. else
  296. {
  297. $this->form->clear(TRUE);
  298. }
  299. }
  300. catch (Exception $e) // in case of exception
  301. {
  302. new TMessage('error', $e->getMessage()); // shows the exception error message
  303. TTransaction::rollback(); // undo all pending operations
  304. }
  305. }
  306. public static function onClose()
  307. {
  308. parent::closeWindow();
  309. }
  310. }

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


NR

Teste o seguinte na função onEdit:
 
  1. <?php
  2. // onEdit
  3. $data->spath = ProdutoImagem::where('produto_id', '=', $param['key'])->getIndexedArray('id','spath');
  4. ?>
PG

Obrigado.
Deu certo.
valeu.