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"
------------------------------------------------------------------------------------------------------------------
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"
------------------------------------------------------------------------------------------------------------------
- <?php
- /**
- * Produto Active Record
- * @author <your-name-here>
- */
- class Produto extends TRecord
- {
- const TABLENAME = '"public"."produto"';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'max'; // {max, serial}
- const CREATEDAT = 'created_at';
- const UPDATEDAT = 'updated_at';
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('usuarioinclusao_id');
- parent::addAttribute('usuarioalteracao_id');
- parent::addAttribute('unidade_id');
- parent::addAttribute('sdescricao');
- parent::addAttribute('sdescricaonf');
- parent::addAttribute('sreferencia');
- parent::addAttribute('created_at');
- parent::addAttribute('updated_at');
- parent::addAttribute('marca_id');
- parent::addAttribute('grupoproduto_id');
- parent::addAttribute('npesoliquido');
- parent::addAttribute('npesobruto');
- parent::addAttribute('nprecocusto');
- parent::addAttribute('nprecovenda');
- parent::addAttribute('nporcentagemlucrodesejado');
- parent::addAttribute('nlucrodesejado');
- parent::addAttribute('nprecosugerido');
- parent::addAttribute('fornecedor_id');
- parent::addAttribute('stipoproduto');
- parent::addAttribute('nprecopromocao');
- parent::addAttribute('sativapromocao');
- parent::addAttribute('nestoqueminimo');
- parent::addAttribute('nestoquemaximo');
- parent::addAttribute('nestoquesaldo');
- parent::addAttribute('sespecificacao');
- parent::addAttribute('scodigogtin');
- parent::addAttribute('scodigoanp');
- parent::addAttribute('sippt');
- parent::addAttribute('siat');
- parent::addAttribute('slocalizacaocorredor');
- parent::addAttribute('slocalizacaoposicaohorizontal');
- parent::addAttribute('slocalizacaoposicaovertical');
- parent::addAttribute('ncm_id');
- parent::addAttribute('scodigogtintrib');
- parent::addAttribute('dt_ultimacompra');
- }
- public function get_unidade()
- {
- return Unidade::find($this->unidade_id);
- }
- public function get_marca()
- {
- return Marca::find($this->marca_id);
- }
- public function get_grupoproduto()
- {
- return Grupoproduto::find($this->grupoproduto_id);
- }
- public function get_fornecedor()
- {
- return Pessoa::find($this->fornecedor_id);
- }
- public function get_ncm()
- {
- return Ncm::where(scodigoncm,'=',$this->ncm_id)->load()->first();
- }
- }
- <?php
- /**
- * ProdutoImagem Active Record
- * @author <your-name-here>
- */
- class ProdutoImagem extends TRecord
- {
- const TABLENAME = '"public"."produto_imagem"';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'max'; // {max, serial}
- const CREATEDAT = 'created_at';
- const UPDATEDAT = 'updated_at';
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('produto_id');
- parent::addAttribute('spath');
- parent::addAttribute('created_at');
- parent::addAttribute('updated_at');
- }
- }
- <?php
- /**
- * ProdutoForm Form
- * @author <your name here>
- */
- class ProdutoForm extends TWindow
- {
- protected $form; // form
- use Adianti\Base\AdiantiFileSaveTrait;
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- parent::include_js('app/lib/include/application.js');
- parent::setSize(0.8, 0.80);
- parent::removePadding();
- parent::removeTitleBar();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_Produto');
- $this->form->setFormTitle('Produto');
- // create the form fields
- $id = new TEntry('id');
- $usuarioinclusao_id = new TEntry('usuarioinclusao_id');
- $usuarioalteracao_id = new TEntry('usuarioalteracao_id');
- $unidade_id = new TDBUniqueSearch('unidade_id', 'tutorerpdfe', 'Unidade', 'id', 'sdescricao');
- $sdescricao = new TEntry('sdescricao');
- $sdescricaonf = new TEntry('sdescricaonf');
- $sreferencia = new TEntry('sreferencia');
- $marca_id = new TDBUniqueSearch('marca_id', 'tutorerpdfe', 'Marca', 'id', 'snome');
- $grupoproduto_id = new TDBUniqueSearch('grupoproduto_id', 'tutorerpdfe', 'Grupoproduto', 'id', 'snome');
- $npesoliquido = new TEntry('npesoliquido');
- $npesobruto = new TEntry('npesobruto');
- $nprecocusto = new TEntry('nprecocusto');
- $nprecovenda = new TEntry('nprecovenda');
- $nporcentagemlucrodesejado = new TEntry('nporcentagemlucrodesejado');
- $nlucrodesejado = new TEntry('nlucrodesejado');
- $nprecosugerido = new TEntry('nprecosugerido');
- $fornecedor_id = new TDBUniqueSearch('fornecedor_id', 'tutorerpdfe', 'Pessoa', 'id', 'nome');
- $stipoproduto = new TCombo('stipoproduto');
- $nprecopromocao = new TEntry('nprecopromocao');
- $sativapromocao = new TCombo('sativapromocao');
- $nestoqueminimo = new TEntry('nestoqueminimo');
- $nestoquemaximo = new TEntry('nestoquemaximo');
- $nestoquesaldo = new TEntry('nestoquesaldo');
- $sespecificacao = new TText('sespecificacao');
- $scodigogtin = new TEntry('scodigogtin');
- $scodigoanp = new TEntry('scodigoanp');
- $sippt = new TCombo('sippt');
- $siat = new TCombo('siat');
- $slocalizacaocorredor = new TEntry('slocalizacaocorredor');
- $slocalizacaoposicaohorizontal = new TEntry('slocalizacaoposicaohorizontal');
- $slocalizacaoposicaovertical = new TEntry('slocalizacaoposicaovertical');
- $ncm_id = new TDBUniqueSearch('ncm_id', 'tutorerpdfe', 'Ncm', 'scodigoncm', 'scodigoncm');
- $scodigogtintrib = new TEntry('scodigogtintrib');
- $dt_ultimacompra = new TDate('dt_ultimacompra');
- $spath = new TMultiFile('spath');
- $this->form->addField( $usuarioinclusao_id );
- $this->form->addField( $usuarioalteracao_id );
- // add the fields
- $row=$this->form->addFields( [ new TLabel('Id') ], [ $id ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Descricao','red') ], [ $sdescricao ] , [ new TLabel('Descricao NFe','red') ], [ $sdescricaonf ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Referencia','red') ], [ $sreferencia ] , [ new TLabel('Unidade','red') ], [ $unidade_id ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Marca','red') ], [ $marca_id ] , [ new TLabel('Grupo Produto','red') ], [ $grupoproduto_id ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('R$ Custo') ], [ $nprecocusto ] , [ new TLabel('R$ Venda') ], [ $nprecovenda ]);
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Peso Liq.') ], [ $npesoliquido ] , [ new TLabel('Peso Br.') ], [ $npesobruto ]
- );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('% Lucro Desejado') ], [ $nporcentagemlucrodesejado ] ,
- [ new TLabel('Lucro Desejado') ], [ $nlucrodesejado ] ,
- [ new TLabel('Preco Sugerido') ], [ $nprecosugerido ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Fornecedor','red') ], [ $fornecedor_id ] ,[ new TLabel('Dt Ultima Compra') ], [ $dt_ultimacompra ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Tipo Produto','red') ], [ $stipoproduto ] ,
- [ new TLabel('Preco Promoção') ], [ $nprecopromocao ] ,
- [ new TLabel('Promoção Ativa') ], [ $sativapromocao ] );
- $row->layout = [ 'col-sm-2 control-label', 'col-sm-3',
- 'col-sm-2 control-label', 'col-sm-2',
- 'col-sm-2 control-label', 'col-sm-1' ];
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Estoque Minimo') ], [ $nestoqueminimo ] ,
- [ new TLabel('Estoque Maximo') ], [ $nestoquemaximo ] ,
- [ new TLabel('Estoque Saldo') ], [ $nestoquesaldo ] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields(
- [ new TLabel('Codigo ANP') ], [ $scodigoanp ] ,
- [ new TLabel('SIAT') ], [ $siat ], [ new TLabel('IPPT') ], [ $sippt ] ,[new TLabel('Imagem')], [$spath] );
- $row->layout = [ 'col-sm-2 control-label', 'col-sm-1',
- 'col-sm-2 control-label', 'col-sm-2',
- 'col-sm-1 control-label', 'col-sm-1',
- 'col-sm-1 control-label', 'col-sm-2' ];
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Localização Corredor') ], [ $slocalizacaocorredor ] ,
- [ new TLabel('Hizontal') ], [ $slocalizacaoposicaohorizontal ] ,
- [ new TLabel('Vertical') ], [ $slocalizacaoposicaovertical ] ,[]);
- $row->layout = [ 'col-sm-2 control-label', 'col-sm-1',
- 'col-sm-2 control-label', 'col-sm-2',
- 'col-sm-1 control-label', 'col-sm-1',
- 'col-sm-1 control-label', 'col-sm-2' ];
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('NCM','red') ], [ $ncm_id ] , [ new TLabel('Codigo GTIN') ], [ $scodigogtin ] , [ new TLabel('GTIN Trib') ], [ $scodigogtintrib ] ,[]);
- $row->layout = [ 'col-sm-2 control-label', 'col-sm-2',
- 'col-sm-2 control-label', 'col-sm-2',
- 'col-sm-1 control-label', 'col-sm-1',
- 'col-sm-1 control-label', 'col-sm-1' ];
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ new TLabel('Especificação') ], [ $sespecificacao ],[] );
- $row->class = 'form-control-sm';
- $row=$this->form->addFields( [ ], [ ],[] );
- $row->class = 'form-control-sm';
- // set sizes
- $id->setSize('100%');
- $usuarioinclusao_id->setSize('100%');
- $usuarioalteracao_id->setSize('100%');
- $unidade_id->setSize('100%');
- $sdescricao->setSize('100%');
- $sdescricaonf->setSize('100%');
- $sreferencia->setSize('100%');
- $marca_id->setSize('100%');
- $grupoproduto_id->setSize('100%');
- $npesoliquido->setSize('100%');
- $npesobruto->setSize('100%');
- $nprecocusto->setSize('100%');
- $nprecovenda->setSize('100%');
- $nporcentagemlucrodesejado->setSize('100%');
- $nlucrodesejado->setSize('100%');
- $nprecosugerido->setSize('100%');
- $fornecedor_id->setSize('100%');
- $stipoproduto->setSize('100%');
- $nprecopromocao->setSize('100%');
- $sativapromocao->setSize('100%');
- $nestoqueminimo->setSize('100%');
- $nestoquemaximo->setSize('100%');
- $nestoquesaldo->setSize('100%');
- $sespecificacao->setSize('100%');
- $scodigogtin->setSize('100%');
- $scodigoanp->setSize('100%');
- $sippt->setSize('100%');
- $siat->setSize('100%');
- $slocalizacaocorredor->setSize('100%');
- $slocalizacaoposicaohorizontal->setSize('100%');
- $slocalizacaoposicaovertical->setSize('100%');
- $ncm_id->setSize('100%');
- $scodigogtintrib->setSize('100%');
- $dt_ultimacompra->setSize('100%');
- $sespecificacao->setSize(730,60);
- $spath->setAllowedExtensions( ['gif', 'png', 'jpg', 'jpeg'] );
- $spath->enableFileHandling();
- $spath->enableImageGallery();
- $spath->enablePopover('Preview', '<img style="max-width:250px" src="download.php?file={file_name}">');
- $sippt->addItems( ['P' => 'PROPRIO', 'T' => 'TERCEIROS'] );
- $sippt->setValue('P');
- $siat->addItems( ['A' => 'ARREDONDAMENTO', 'T' => 'TRUNCAMENTO'] );
- $siat->setValue('A');
- $stipoproduto->addItems( ['PR' => 'PROPRIO REVENDA', 'TR' => 'TERCEIROS REVENDA', 'CO' => 'CONSUMO', 'MP' => 'MATÉRIA PRIMA' ] );
- $stipoproduto->setValue('TR');
- $sativapromocao->addItems( ['N' => 'NÃO', 'S' => 'SIM' ] );
- $sativapromocao->setValue('N');
- $dt_ultimacompra->setMask('dd/mm/yyyy',true);
- $dt_ultimacompra->setDatabaseMask('yyyy-mm-dd');
- $dt_ultimacompra->setOption('showOnFocus', false);
- $dt_ultimacompra->setOption('triggerEvent', 'dblclick');
- $npesoliquido->setNumericMask(4, ',', '.', true);
- $npesobruto->setNumericMask(4, ',', '.', true);
- $nprecocusto->setNumericMask(4, ',', '.', true);
- $nprecovenda->setNumericMask(4, ',', '.', true);
- $nporcentagemlucrodesejado->setNumericMask(4, ',', '.', true);
- $nlucrodesejado->setNumericMask(4, ',', '.', true);
- $nprecosugerido->setNumericMask(4, ',', '.', true);
- $nprecopromocao->setNumericMask(4, ',', '.', true);
- $nestoqueminimo->setNumericMask(4, ',', '.', true);
- $nestoquemaximo->setNumericMask(4, ',', '.', true);
- $nestoquesaldo->setNumericMask(4, ',', '.', true);
- $npesoliquido->setValue('0');
- $npesobruto->setValue('0');
- $nprecocusto->setValue('0');
- $nprecovenda->setValue('0');
- $nporcentagemlucrodesejado->setValue('0');
- $nlucrodesejado->setValue('0');
- $nprecosugerido->setValue('0');
- $nprecopromocao->setValue('0');
- $nestoqueminimo->setValue('0');
- $nestoquemaximo->setValue('0');
- $nestoquesaldo->setValue('0');
- $unidade_id->setMinLength(0);
- $marca_id->setMinLength(0);
- $grupoproduto_id->setMinLength(0);
- $sdescricao->forceUpperCase();
- $sdescricaonf->forceUpperCase();
- $sreferencia->forceUpperCase();
- $slocalizacaocorredor->forceUpperCase();
- $slocalizacaoposicaohorizontal->forceUpperCase();
- $slocalizacaoposicaovertical->forceUpperCase();
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- /** samples
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( '100%' ); // set size
- **/
- $this->form->addHeaderActionLink( _t('Close'), new TAction([__CLASS__, 'onClose'], ['static'=>'1']), 'fa:times red');
- // create the form actions
- $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('tutorerpdfe'); // open a transaction
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
- $this->form->validate(); // validate form data
- $data = $this->form->getData(); // get form data as array
- $object = new Produto; // create an empty object
- $object->fromArray( (array) $data); // load the object with data
- if (!empty($object->id)) {
- $objectImagem = new ProdutoImagem;
- $objectImagem->delete($object->id);
- }
- $this->saveFiles($object, $data, 'spath', 'files/images', 'ProdutoImagem', 'spath', 'produto_id');
- if (empty($object->id)) {
- $object->usuarioinclusao_id = TSession::getValue('userid');
- $object->usuarioalteracao_id = '0';
- } else {
- $object->usuarioalteracao_id = TSession::getValue('userid');
- }
- if (empty($object->fornecedor_id)) {
- $object->fornecedor_id = '0';
- }
- $object->store(); // save the object
- // get the generated id
- $data->id = $object->id;
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
- new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(TRUE);
- }
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('tutorerpdfe'); // open a transaction
- $object = new Produto($key); // instantiates the Active Record
- $this->form->setData($object); // fill the form
- $data = new stdClass;
- $data->spath = ProdutoImagem::where('produto_id', '=', $param['key'])->getIndexedArray('spath');
- $this->form->setData($data);
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear(TRUE);
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- public static function onClose()
- {
- parent::closeWindow();
- }
- }
Teste o seguinte na função onEdit:
Obrigado.
Deu certo.
valeu.