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

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 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!