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.
Utilizo o Adianti studio PRO.
- <?php
- /**
- * AirlineForm Master/Detail
- * @author
- */
- class AirlineForm extends TPage
- {
- protected $form; // form
- protected $fieldlist;
- // trait with saveFile, saveFiles, ...
- use Adianti\Base\AdiantiFileSaveTrait;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct($param)
- {
- parent::__construct($param);
- // creates the form
- $this->form = new TForm('form_Airline');
- $panel_master = new TPanelGroup('Airline');
- $vbox = new TVBox;
- $vbox->style = 'width: 100%';
- $this->form->add($panel_master);
- $panel_master->add($vbox);
- $table_general = new TTable;
- $table_general->width = '100%';
- $frame_general = new TFrame;
- $frame_general->class = 'tframe tframe-custom';
- $frame_general->setLegend(_t('Airline'));
- $frame_general->style = 'background:whiteSmoke';
- $frame_general->add($table_general);
- $frame_details = new TFrame;
- $frame_details->class = 'tframe tframe-custom';
- $frame_details->setLegend(_t('Flights'));
- $vbox->add( $frame_general );
- $vbox->add( $frame_details );
- // master fields
- $id = new TEntry('id');
- $a2 = new TEntry('a2');
- $a3 = new TEntry('a3');
- $name = new TEntry('name');
- $photo_path = new TFile('photo_path');
- // sizes
- $id->setSize('100%');
- $a2->setSize('100%');
- $a3->setSize('100%');
- $name->setSize('100%');
- $photo_path->setSize('100%');
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- // add form fields to be handled by form
- $this->form->addField($id);
- $this->form->addField($a2);
- $this->form->addField($a3);
- $this->form->addField($name);
- $this->form->addField($photo_path);
- // add form fields to the screen
- $table_general->addRowSet( new TLabel('Id'), $id );
- $table_general->addRowSet( new TLabel('A2'), $a2 );
- $table_general->addRowSet( new TLabel('A3'), $a3 );
- $table_general->addRowSet( new TLabel('Name'), $name );
- $table_general->addRowSet( new TLabel('Photo Path'), $photo_path );
- // detail fields
- $this->fieldlist = new TFieldList;
- $this->fieldlist->enableSorting();
- $frame_details->add($this->fieldlist);
- $number = new TEntry('list_number[]');
- $origem_id = new TDBCombo('list_origem_id[]', 'atendimento', 'Aerodrome', 'id', 'iata');
- $destino_id = new TDBCombo('list_destino_id[]', 'atendimento', 'Aerodrome', 'id', 'iata');
- $number->setSize('100%');
- $origem_id->setSize('100%');
- $destino_id->setSize('100%');
- $this->fieldlist->addField( '<b>Number</b>', $number, ['width' => '20%']);
- $this->fieldlist->addField( '<b>Origem</b>', $origem_id, ['width' => '20%']);
- $this->fieldlist->addField( '<b>Destino</b>', $destino_id, ['width' => '20%']);
- $this->form->addField($number);
- $this->form->addField($origem_id);
- $this->form->addField($destino_id);
- // create an action button (save)
- $save_button = TButton::create('save', [$this, 'onSave'], _t('Save'), 'fa:save blue');
- $new_button = TButton::create('new', [$this, 'onClear'], _t('Clear'), 'fa:eraser red');
- $this->form->addField($save_button);
- $this->form->addField($new_button);
- $panel_master->addFooter( THBox::pack($save_button, $new_button) );
- // create the page container
- $container = new TVBox;
- $container->style = 'width: 100%';
- //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- TTransaction::open('atendimento');
- if (isset($param['key']))
- {
- $key = $param['key'];
- $object = new Airline($key);
- $this->form->setData($object);
- $items = Flight::where('airline_id', '=', $key)->load();
- $this->fieldlist->addHeader();
- if ($items)
- {
- foreach($items as $item )
- {
- $detail = new stdClass;
- $detail->list_number = $item->number;
- $detail->list_origem_id = $item->origem_id;
- $detail->list_destino_id = $item->destino_id;
- $this->fieldlist->addDetail($detail);
- }
- $this->fieldlist->addCloneAction();
- }
- else
- {
- $this->onClear($param);
- }
- TTransaction::close(); // close transaction
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * Clear form
- */
- public function onClear($param)
- {
- $this->fieldlist->addHeader();
- $this->fieldlist->addDetail( new stdClass );
- $this->fieldlist->addCloneAction();
- }
- /**
- * Save the Airline and the Flight's
- */
- public static function onSave($param)
- {
- try
- {
- TTransaction::open('atendimento');
- $id = (int) $param['id'];
- $master = new Airline;
- $master->fromArray( $param);
- $master->store(); // save master object
- // copy file to target folder
- $this->saveFile($master, $param, 'photo_path', 'files/images');
- // delete details
- Flight::where('airline_id', '=', $master->id)->delete();
- if( !empty($param['list_number']) AND is_array($param['list_number']) )
- {
- foreach( $param['list_number'] as $row => $number)
- {
- if (!empty($number))
- {
- $detail = new Flight;
- $detail->airline_id = $master->id;
- $detail->number = $param['list_number'][$row];
- $detail->origem_id = $param['list_origem_id'][$row];
- $detail->destino_id = $param['list_destino_id'][$row];
- $detail->store();
- }
- }
- }
- $data = new stdClass;
- $data->id = $master->id;
- TForm::sendData('form_Airline', $data);
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- }
- ?>
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
Estou com a versão 5.6.0 do Framework.
Não deu certo deste jeito!
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();
Apareceu este erro ao salvar:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'photo_path' at row 1
Apareceu este erro ao salvar:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'photo_path' at row 1
está dando erro no campo do banco de dados muda ele de char para text somente para testarmos.
Já mudei e agora voltou o erro anterior na linha 189.
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);
}
Deu certo desta forma.
Muito obrigado pela ajuda!
Vou dar uma estudada neste componente.
Um grande abraço!