PROBLEMAS AO MOVER ARQUIVO COM TFILE FORM MESTRE DETALHES Não estou conseguindo mover o arquivo (imagem) para a pasta especificada. Utilizo o Adianti studio PRO. ...
AR
PROBLEMAS AO MOVER ARQUIVO COM TFILE FORM MESTRE DETALHES  
Não estou conseguindo mover o arquivo (imagem) para a pasta especificada.
Utilizo o Adianti studio PRO.

 
  1. <?php
  2. /**
  3. * AirlineForm Master/Detail
  4. * @author
  5. */
  6. class AirlineForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $fieldlist;
  10. // trait with saveFile, saveFiles, ...
  11. use Adianti\Base\AdiantiFileSaveTrait;
  12. /**
  13. * Class constructor
  14. * Creates the page and the registration form
  15. */
  16. function __construct($param)
  17. {
  18. parent::__construct($param);
  19. // creates the form
  20. $this->form = new TForm('form_Airline');
  21. $panel_master = new TPanelGroup('Airline');
  22. $vbox = new TVBox;
  23. $vbox->style = 'width: 100%';
  24. $this->form->add($panel_master);
  25. $panel_master->add($vbox);
  26. $table_general = new TTable;
  27. $table_general->width = '100%';
  28. $frame_general = new TFrame;
  29. $frame_general->class = 'tframe tframe-custom';
  30. $frame_general->setLegend(_t('Airline'));
  31. $frame_general->style = 'background:whiteSmoke';
  32. $frame_general->add($table_general);
  33. $frame_details = new TFrame;
  34. $frame_details->class = 'tframe tframe-custom';
  35. $frame_details->setLegend(_t('Flights'));
  36. $vbox->add( $frame_general );
  37. $vbox->add( $frame_details );
  38. // master fields
  39. $id = new TEntry('id');
  40. $a2 = new TEntry('a2');
  41. $a3 = new TEntry('a3');
  42. $name = new TEntry('name');
  43. $photo_path = new TFile('photo_path');
  44. // sizes
  45. $id->setSize('100%');
  46. $a2->setSize('100%');
  47. $a3->setSize('100%');
  48. $name->setSize('100%');
  49. $photo_path->setSize('100%');
  50. if (!empty($id))
  51. {
  52. $id->setEditable(FALSE);
  53. }
  54. // add form fields to be handled by form
  55. $this->form->addField($id);
  56. $this->form->addField($a2);
  57. $this->form->addField($a3);
  58. $this->form->addField($name);
  59. $this->form->addField($photo_path);
  60. // add form fields to the screen
  61. $table_general->addRowSet( new TLabel('Id'), $id );
  62. $table_general->addRowSet( new TLabel('A2'), $a2 );
  63. $table_general->addRowSet( new TLabel('A3'), $a3 );
  64. $table_general->addRowSet( new TLabel('Name'), $name );
  65. $table_general->addRowSet( new TLabel('Photo Path'), $photo_path );
  66. // detail fields
  67. $this->fieldlist = new TFieldList;
  68. $this->fieldlist->enableSorting();
  69. $frame_details->add($this->fieldlist);
  70. $number = new TEntry('list_number[]');
  71. $origem_id = new TDBCombo('list_origem_id[]', 'atendimento', 'Aerodrome', 'id', 'iata');
  72. $destino_id = new TDBCombo('list_destino_id[]', 'atendimento', 'Aerodrome', 'id', 'iata');
  73. $number->setSize('100%');
  74. $origem_id->setSize('100%');
  75. $destino_id->setSize('100%');
  76. $this->fieldlist->addField( '<b>Number</b>', $number, ['width' => '20%']);
  77. $this->fieldlist->addField( '<b>Origem</b>', $origem_id, ['width' => '20%']);
  78. $this->fieldlist->addField( '<b>Destino</b>', $destino_id, ['width' => '20%']);
  79. $this->form->addField($number);
  80. $this->form->addField($origem_id);
  81. $this->form->addField($destino_id);
  82. // create an action button (save)
  83. $save_button = TButton::create('save', [$this, 'onSave'], _t('Save'), 'fa:save blue');
  84. $new_button = TButton::create('new', [$this, 'onClear'], _t('Clear'), 'fa:eraser red');
  85. $this->form->addField($save_button);
  86. $this->form->addField($new_button);
  87. $panel_master->addFooter( THBox::pack($save_button, $new_button) );
  88. // create the page container
  89. $container = new TVBox;
  90. $container->style = 'width: 100%';
  91. //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  92. $container->add($this->form);
  93. parent::add($container);
  94. }
  95. /**
  96. * Executed whenever the user clicks at the edit button da datagrid
  97. */
  98. function onEdit($param)
  99. {
  100. try
  101. {
  102. TTransaction::open('atendimento');
  103. if (isset($param['key']))
  104. {
  105. $key = $param['key'];
  106. $object = new Airline($key);
  107. $this->form->setData($object);
  108. $items = Flight::where('airline_id', '=', $key)->load();
  109. $this->fieldlist->addHeader();
  110. if ($items)
  111. {
  112. foreach($items as $item )
  113. {
  114. $detail = new stdClass;
  115. $detail->list_number = $item->number;
  116. $detail->list_origem_id = $item->origem_id;
  117. $detail->list_destino_id = $item->destino_id;
  118. $this->fieldlist->addDetail($detail);
  119. }
  120. $this->fieldlist->addCloneAction();
  121. }
  122. else
  123. {
  124. $this->onClear($param);
  125. }
  126. TTransaction::close(); // close transaction
  127. }
  128. }
  129. catch (Exception $e) // in case of exception
  130. {
  131. new TMessage('error', $e->getMessage());
  132. TTransaction::rollback();
  133. }
  134. }
  135. /**
  136. * Clear form
  137. */
  138. public function onClear($param)
  139. {
  140. $this->fieldlist->addHeader();
  141. $this->fieldlist->addDetail( new stdClass );
  142. $this->fieldlist->addCloneAction();
  143. }
  144. /**
  145. * Save the Airline and the Flight's
  146. */
  147. public static function onSave($param)
  148. {
  149. try
  150. {
  151. TTransaction::open('atendimento');
  152. $id = (int) $param['id'];
  153. $master = new Airline;
  154. $master->fromArray( $param);
  155. $master->store(); // save master object
  156. // copy file to target folder
  157. $this->saveFile($master, $param, 'photo_path', 'files/images');
  158. // delete details
  159. Flight::where('airline_id', '=', $master->id)->delete();
  160. if( !empty($param['list_number']) AND is_array($param['list_number']) )
  161. {
  162. foreach( $param['list_number'] as $row => $number)
  163. {
  164. if (!empty($number))
  165. {
  166. $detail = new Flight;
  167. $detail->airline_id = $master->id;
  168. $detail->number = $param['list_number'][$row];
  169. $detail->origem_id = $param['list_origem_id'][$row];
  170. $detail->destino_id = $param['list_destino_id'][$row];
  171. $detail->store();
  172. }
  173. }
  174. }
  175. $data = new stdClass;
  176. $data->id = $master->id;
  177. TForm::sendData('form_Airline', $data);
  178. TTransaction::close(); // close the transaction
  179. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  180. }
  181. catch (Exception $e) // in case of exception
  182. {
  183. new TMessage('error', $e->getMessage());
  184. TTransaction::rollback();
  185. }
  186. }
  187. }
  188. ?>

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


FC

Qual a versão do framework?
Tenta assim
$this->saveFile($master, $master, 'photo_path', 'files/images');

Senão der certo pode fazer a mão mesmo com rename sem o trait

AR

Estou com a versão 5.6.0 do Framework.
Não deu certo deste jeito!
FC

Altera lá em cima, senão der certo vamos fazer a mão sem o trait
$photo_path = new TFile('photo_path');
$photo_path->enableFileHandling();
AR

Apareceu este erro ao salvar:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'photo_path' at row 1
AR

Apareceu este erro ao salvar:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'photo_path' at row 1
FC

está dando erro no campo do banco de dados muda ele de char para text somente para testarmos.
AR

Já mudei e agora voltou o erro anterior na linha 189.
FC

então o saveFile espera como parametro o $data do form que é um array diferente do $param se quiser depois se aprofundar e estudar esses componentes é legal porém apenas para resolver de imediato retire a linha
$photo_path->enableFileHandling();
e onde está a linha
$this->saveFile($master, $master, 'photo_path', 'files/images');
altere para:
$source_file = 'tmp/'.$master->photo_path;
if(file_exists($source_file)){
$target_file = 'files/images/'.$master->photo_path;
rename($source_file, $target_file);
}

AR

Deu certo desta forma.

Muito obrigado pela ajuda!

Vou dar uma estudada neste componente.

Um grande abraço!