TMultiFile Pessoal, estou querendo salvar o meu banco de dados os arquivos puxado pelo TMultiFile, porém não estou conseguindo, algué já passou por isso e como resolver? segue meu código feito pelo Adianti Studio; ...
FS
TMultiFile  
Pessoal, estou querendo salvar o meu banco de dados os arquivos puxado pelo TMultiFile, porém não estou conseguindo,
algué já passou por isso e como resolver?
segue meu código feito pelo Adianti Studio;

 
  1. <?php
  2. /**
  3. * ArquivosForm Form
  4. * @author <your name here>
  5. */
  6. class ArquivosForm extends TPage
  7. {
  8. protected $form; // form
  9. /**
  10. * Form constructor
  11. * @param $param Request
  12. */
  13. public function __construct( $param )
  14. {
  15. parent::__construct();
  16. // creates the form
  17. $this->form = new BootstrapFormBuilder('form_Arquivos');
  18. $this->form->setFormTitle('Arquivos');
  19. // create the form fields
  20. //$idarquivos = new TEntry('idarquivos');
  21. $arquivo = new TMultiFile('arquivo');
  22. $idconvenio = new TEntry('idconvenio');
  23. $datainclusao = new TDate('datainclusao');
  24. $datalancamento = new TDate('datalancamento');
  25. $tamanho = new TEntry('tamanho');
  26. // add the fields
  27. //$this->form->addFields( [ new TLabel('Idarquivos') ], [ $idarquivos ] );
  28. //$this->form->addFields( [ new TLabel('Idconvenio') ], [ $idconvenio ] );
  29. $this->form->addFields( [ new TLabel('Arquivo') ], [ $arquivo ] );
  30. //$this->form->addFields( [ new TLabel('Datainclusao') ], [ $datainclusao ] );
  31. //$this->form->addFields( [ new TLabel('Datalancamento') ], [ $datalancamento ] );
  32. //$this->form->addFields( [ new TLabel('Tamanho') ], [ $tamanho ] );
  33. // set sizes
  34. //$idarquivos->setSize('100%');
  35. //$idconvenio->setSize('100%');
  36. $arquivo->setSize('100%');
  37. //$datainclusao->setSize('100%');
  38. //$datalancamento->setSize('100%');
  39. //$tamanho->setSize('100%');
  40. if (!empty($idarquivos))
  41. {
  42. $idarquivos->setEditable(FALSE);
  43. }
  44. /** samples
  45. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  46. $fieldX->setSize( '100%' ); // set size
  47. **/
  48. // create the form actions
  49. $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
  50. $btn->class = 'btn btn-sm btn-primary';
  51. $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
  52. // vertical box container
  53. $container = new TVBox;
  54. $container->style = 'width: 100%';
  55. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  56. $container->add($this->form);
  57. parent::add($container);
  58. }
  59. /**
  60. * Save form data
  61. * @param $param Request
  62. */
  63. public function onSave( $param )
  64. {
  65. try
  66. {
  67. TTransaction::open('chronos'); // open a transaction
  68. /**
  69. // Enable Debug logger for SQL operations inside the transaction
  70. TTransaction::setLogger(new TLoggerSTD); // standard output
  71. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  72. **/
  73. $this->form->validate(); // validate form data
  74. $data = $this->form->getData(); // get form data as array
  75. $object = new Arquivos; // create an empty object
  76. $object->fromArray( (array) $data); // load the object with data
  77. $object->store(); // save the object
  78. // get the generated idarquivos
  79. $data->idarquivos = $object->idarquivos;
  80. $this->form->setData($data); // fill form data
  81. TTransaction::close(); // close the transaction
  82. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  83. }
  84. catch (Exception $e) // in case of exception
  85. {
  86. new TMessage('error', $e->getMessage()); // shows the exception error message
  87. $this->form->setData( $this->form->getData() ); // keep form data
  88. TTransaction::rollback(); // undo all pending operations
  89. }
  90. }
  91. /**
  92. * Clear form data
  93. * @param $param Request
  94. */
  95. public function onClear( $param )
  96. {
  97. $this->form->clear(TRUE);
  98. }
  99. /**
  100. * Load object to form data
  101. * @param $param Request
  102. */
  103. public function onEdit( $param )
  104. {
  105. try
  106. {
  107. if (isset($param['key']))
  108. {
  109. $key = $param['key']; // get the parameter $key
  110. TTransaction::open('chronos'); // open a transaction
  111. $object = new Arquivos($key); // instantiates the Active Record
  112. $this->form->setData($object); // fill the form
  113. TTransaction::close(); // close the transaction
  114. }
  115. else
  116. {
  117. $this->form->clear(TRUE);
  118. }
  119. }
  120. catch (Exception $e) // in case of exception
  121. {
  122. new TMessage('error', $e->getMessage()); // shows the exception error message
  123. TTransaction::rollback(); // undo all pending operations
  124. }
  125. }
  126. }

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


NR

https://adianti.com.br/framework_files/tutor/index.php?class=ProductForm&method=onEdit
FS

Este exemplo eu cheguei a ver porém ele não salva no bando de dados os nomes dos arquivos upados.
NR

A função saveFiles(chamada na onSave) faz exatamente isso:
 
  1. <?php
  2. // onSave
  3. $this->saveFiles($object, $data, 'images', 'files/images', 'ProductImage', 'image', 'product_id');
  4. ?>

Se não estiver gravando corretamente, confira os parâmetros passados:
 
  1. <?php
  2. /**
  3. * Save files
  4. * @param $object Active Record
  5. * @param $data Form data
  6. * @param $input_name Input field name
  7. * @param $target_path Target file path
  8. * @param $model_files Files Active Record
  9. * @param $file_field File field in model_files
  10. * @param $foreign_key Foreign key to $object
  11. */
  12. public function saveFiles($object, $data, $input_name, $target_path, $model_files, $file_field, $foreign_key)
  13. ?>
FS

agora esta dando este erro:
urldecode() expects parameter 1 to be string, array given in /lib/adianti/base/AdiantiFileSaveTrait.php on line 28
FS

$this->saveFile($object, $data, 'arquivo', 'files/convenios');
Coloquei desta forma, não sei que parametro é este que esta faltando.
FS

Consegue resolver o saveFile ou seja enviando somente um arquivo, agora no saveFiles que posso enviar varios arquivos este não funciona.