ST
Relacionamento entre tabelas, limpar campo e inserir imagens
Amigos,
Sou iniciante no Adianti Framework e estou com algumas dificuldades tais como: fazer o relacionamento entre tabelas, gravar imagem no banco de dados e limpar o campo de pesquisa após realizar consulta.
Preciso que me ajudem nessa fase inicial.
Segue os modelos:
Sou iniciante no Adianti Framework e estou com algumas dificuldades tais como: fazer o relacionamento entre tabelas, gravar imagem no banco de dados e limpar o campo de pesquisa após realizar consulta.
Preciso que me ajudem nessa fase inicial.
Segue os modelos:
- <?php
- /**
- * Perdcomp Active Record
- * @author <your-name-here>
- */
- class Perdcomp extends TRecord
- {
- const TABLENAME = 'perdcomp';
- const PRIMARYKEY= 'perdcomp_id';
- const IDPOLICY = 'max'; // {max, serial}
-
-
- private $origem_documento;
- private $tipo_credito;
- private $tipo_documento;
- private $tipo_imposto;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('DataCriacao');
- parent::addAttribute('DataTransmissao');
- parent::addAttribute('Sequencial');
- parent::addAttribute('Nr_PERDCOMP');
- parent::addAttribute('TipoDocumento_id');
- parent::addAttribute('OrigemDocumento_id');
- parent::addAttribute('TipoCredito_id');
- parent::addAttribute('SaldoDeclarado');
- parent::addAttribute('Exercicio');
- parent::addAttribute('TipoImposto_ID');
- parent::addAttribute('ValorCompensado');
- parent::addAttribute('Competencia');
- parent::addAttribute('PERDCOMP_Original');
- parent::addAttribute('Situacao');
- parent::addAttribute('Coluna3');
- parent::addAttribute('Cancelamento');
- parent::addAttribute('CreditoOriginalInicial');
- parent::addAttribute('CreditoOriginalUtilizadoCompensacoes');
- parent::addAttribute('CreditoOriginalDisponivel');
- parent::addAttribute('CreditoOriginalTransmissao');
- parent::addAttribute('SelicAcumulada');
- parent::addAttribute('CreditoAtualizado');
- parent::addAttribute('DebitosDocumento');
- parent::addAttribute('CreditoOriginalUtilizadoDocumento');
- parent::addAttribute('SaldoCreditoOriginal');
- parent::addAttribute('GED');
- }
-
- /**
- * Method set_origem_documento
- * Sample of usage: $perdcomp->origem_documento = $object;
- * @param $object Instance of OrigemDocumento
- */
- public function set_origem_documento(OrigemDocumento $object)
- {
- $this->origem_documento = $object;
- $this->origem_documento_id = $object->id;
- }
-
- /**
- * Method get_origem_documento
- * Sample of usage: $perdcomp->origem_documento->attribute;
- * @returns OrigemDocumento instance
- */
- public function get_origem_documento()
- {
- // loads the associated object
- if (empty($this->origem_documento))
- $this->origem_documento = new OrigemDocumento($this->origem_documento_id);
-
- // returns the associated object
- return $this->origem_documento;
- }
-
-
- /**
- * Method set_tipo_credito
- * Sample of usage: $perdcomp->tipo_credito = $object;
- * @param $object Instance of TipoCredito
- */
- public function set_tipo_credito(TipoCredito $object)
- {
- $this->tipo_credito = $object;
- $this->tipo_credito_id = $object->id;
- }
-
- /**
- * Method get_tipo_credito
- * Sample of usage: $perdcomp->tipo_credito->attribute;
- * @returns TipoCredito instance
- */
- public function get_tipo_credito()
- {
- // loads the associated object
- if (empty($this->tipo_credito))
- $this->tipo_credito = new TipoCredito($this->tipo_credito_id);
-
- // returns the associated object
- return $this->tipo_credito->TipoCreditoDescricao;
- }
-
-
-
-
- /**
- * Method set_tipo_documento
- * Sample of usage: $perdcomp->tipo_documento = $object;
- * @param $object Instance of TipoDocumento
- */
- public function set_tipo_documento(TipoDocumento $object)
- {
- $this->tipo_documento = $object;
- $this->tipo_documento_id = $object->id;
- }
-
- /**
- * Method get_tipo_documento
- * Sample of usage: $perdcomp->tipo_documento->attribute;
- * @returns TipoDocumento instance
- */
- public function get_tipo_documento()
- {
- // loads the associated object
- if (empty($this->tipo_documento))
- $this->tipo_documento = new TipoDocumento($this->tipo_documento_id);
-
- // returns the associated object
- return $this->tipo_documento->TipoDocumentoDescricao;
- }
-
-
- /**
- * Method set_tipo_imposto
- * Sample of usage: $perdcomp->tipo_imposto = $object;
- * @param $object Instance of TipoImposto
- */
- public function set_tipo_imposto(TipoImposto $object)
- {
- $this->tipo_imposto = $object;
- $this->tipo_imposto_id = $object->id;
- }
-
- /**
- * Method get_tipo_imposto
- * Sample of usage: $perdcomp->tipo_imposto->attribute;
- * @returns TipoImposto instance
- */
- public function get_tipo_imposto()
- {
- // loads the associated object
- if (empty($this->tipo_imposto))
- $this->tipo_imposto = new TipoImposto($this->tipo_imposto_id);
-
- // returns the associated object
- return $this->tipo_imposto;
- }
-
- }
- <?php
- /**
- * Tipodocumento Active Record
- * @author <your-name-here>
- */
- class TipoDocumento extends TRecord
- {
- const TABLENAME = 'tipodocumento';
- const PRIMARYKEY= 'TipoDocumento_id';
- const IDPOLICY = 'max'; // {max, serial}
-
-
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('TipoDocumentoDescricao');
- }
- }
- <?php
- /**
- * Tipocredito Active Record
- * @author <your-name-here>
- */
- class TipoCredito extends TRecord
- {
- const TABLENAME = 'tipocredito';
- const PRIMARYKEY= 'TipoCredito_id';
- const IDPOLICY = 'max'; // {max, serial}
-
-
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('TipoCreditoDescricao');
- }
- }
- Segue os formulários:
- <?php
- /**
- * PerdcompForm Registration
- * @author <your name here>
- */
- class PerdcompForm extends TPage
- {
- protected $form; // form
-
- use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods
-
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
-
- $this->setDatabase('perdcomp'); // defines the database
- $this->setActiveRecord('Perdcomp'); // defines the active record
-
- // creates the form
- $this->form = new TQuickForm('form_Perdcomp');
- $this->form->class = 'tform'; // change CSS class
-
- $this->form->style = 'display: table;width:100%'; // change style
-
- // define the form title
- $this->form->setFormTitle('Perdcomp');
-
- // create the form fields
- $perdcomp_id = new TEntry('perdcomp_id');
- $DataCriacao = new TDate('DataCriacao');
- $DataTransmissao = new TDate('DataTransmissao');
- $Sequencial = new TEntry('Sequencial');
- $Nr_PERDCOMP = new TEntry('Nr_PERDCOMP');
- $TipoDocumento_id = new ">TDBSeekButton('TipoDocumento_id', 'perdcomp', $this->form->getName(), 'TipoDocumento', 'TipoDocumentoDescricao', 'TipoDocumento_id', 'TipoDocumentoDescricao');
- $TipodocumentoDescricao = new TEntry('TipoDocumentoDescricao');
- $OrigemdoDocumento_id = new ">TDBSeekButton('OrigemDocumento_id', 'perdcomp', $this->form->getName(), 'OrigemDocumento', 'OrigemDocumentoDescricao', 'OrigemDocumento_id', 'OrigemDocumentoDescricao');
- $OrigemDocumentoDescricao = new TEntry('OrigemDocumentoDescricao');
- $TipoCredito_id = new ">TDBSeekButton('TipoCredito_id', 'perdcomp', $this->form->getName(), 'TipoCredito', 'TipoCreditoDescricao', 'TipoCredito_id', 'TipoCreditoDescricao');
- $TipoCreditoDescricao = new TEntry('TipoCreditoDescricao');
- $SaldoDeclarado = new TEntry('SaldoDeclarado');
- $Exercicio = new TEntry('Exercicio');
- // $TipoImposto_ID = new TSeekButton('TipoImposto_ID');
- $TipoImposto_id = new ">TDBSeekButton('TipoImposto_id', 'perdcomp', $this->form->getName(), 'TipoImposto', 'TipoImposto_Descricao', 'TipoImposto_id', 'TipoImposto_Descricao');
- $TipoImpostoDescricao = new TEntry('TipoImpostoDescricao');
- $ValorCompensado = new TEntry('ValorCompensado');
- $Competencia = new TEntry('Competencia');
- $PERDCOMP_Original = new TEntry('PERDCOMP_Original');
- $Situacao = new TEntry('Situacao');
- $Coluna3 = new TEntry('Coluna3');
- $Cancelamento = new TEntry('Cancelamento');
- $CreditoOriginalInicial = new TEntry('CreditoOriginalInicial');
- $CreditoOriginalUtilizadoCompensacoes = new TEntry('CreditoOriginalUtilizadoCompensacoes');
- $CreditoOriginalDisponivel = new TEntry('CreditoOriginalDisponivel');
- $CreditoOriginalTransmissao = new TEntry('CreditoOriginalTransmissao');
- $SelicAcumulada = new TEntry('SelicAcumulada');
- $CreditoAtualizado = new TEntry('CreditoAtualizado');
- $DebitosDocumento = new TEntry('DebitosDocumento');
- $CreditoOriginalUtilizadoDocumento = new TEntry('CreditoOriginalUtilizadoDocumento');
- $SaldoCreditoOriginal = new TEntry('SaldoCreditoOriginal');
- $GED = new TFile('GED');
- $TipoDocumentoDescricao->setEditable(FALSE);
- $TipoDocumento_id->setSize('40');
- $TipoDocumentoDescricao->setSize('500');
- $OrigemDocumentoDescricao->setEditable(FALSE);
- $OrigemdDcumento_id->setSize('40');
- $OrigemDocumentoDescricao->setSize('500');
- $TipoCreditoDescricao->setEditable(FALSE);
- $TipoCredito_id->setSize('40');
- $TipoCreditoDescricao->setSize('500');
- $TipoImpostoDescricao->setEditable(FALSE);
- $TipoImposto_id->setSize('40');
- $TipoImpostoDescricao->setSize('500');
-
- // add the fields
- $this->form->addQuickField('Código:', $perdcomp_id, 100 );
- $this->form->addQuickField('Data Criação:', $DataCriacao, 100 );
- $this->form->addQuickField('Data Transmissão:', $DataTransmissao, 100 );
- $this->form->addQuickField('Sequencial:', $Sequencial, 200 );
- $this->form->addQuickField('Nº Perdcomp:', $Nr_PERDCOMP, 200 );
- $this->form->addQuickFields('Tipo Documento:', [ $TipoDocumento_id, $TipodocumentoDescricao ] );
- $this->form->addQuickFields('Origem Documento:', [ $OrigemDocumento_id, $OrigemDocumentoDescricao ] );
- $this->form->addQuickFields('Tipo Crédito:', [ $TipoCredito_id, $TipoCreditoDescricao] );
- $this->form->addQuickField('Saldo Declarado:', $SaldoDeclarado, 150 );
- $this->form->addQuickField('Exercicio:', $Exercicio, 100 );
- $this->form->addQuickFields('Tipo Imposto:', [ $TipoImposto_id, $TipoImpostoDescricao] );
- $this->form->addQuickField('Valor Compensado:', $ValorCompensado, 150 );
- $this->form->addQuickField('Competência:', $Competencia, 100 );
- $this->form->addQuickField('Perdcomp Original:', $PERDCOMP_Original, 200 );
- $this->form->addQuickField('Situação:', $Situacao, 200 );
- $this->form->addQuickField('Coluna3:', $Coluna3, 200 );
- $this->form->addQuickField('Cancelamento:', $Cancelamento, 200 );
- $this->form->addQuickField('Crédito Original Inicial:', $CreditoOriginalInicial, 150 );
- $this->form->addQuickField('Crédito Original Utilizado Compensações:', $CreditoOriginalUtilizadoCompensacoes, 150 );
- $this->form->addQuickField('Crédito Original Disponível:', $CreditoOriginalDisponivel, 150 );
- $this->form->addQuickField('Crédito Original Transmissão:', $CreditoOriginalTransmissao, 150 );
- $this->form->addQuickField('Selic Acumulada:', $SelicAcumulada, 150 );
- $this->form->addQuickField('Crédito Atualizado:', $CreditoAtualizado, 150 );
- $this->form->addQuickField('Débitos Documento:', $DebitosDocumento, 150 );
- $this->form->addQuickField('Crédito Original Utilizado Documento:', $CreditoOriginalUtilizadoDocumento, 150, new TRequiredValidator );
- $this->form->addQuickField('Saldo Crédito Original:', $SaldoCreditoOriginal, 150 , new TRequiredValidator);
- $this->form->addQuickField('Ged:', $GED, 500 );
-
- if (!empty($perdcomp_id))
- {
- $perdcomp_id->setEditable(FALSE);
- }
-
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
-
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'bs:plus-sign green');
- $this->form->addQuickAction(_t('Back to the listing'), new TAction(array('PerdcompList', 'onReload')), 'fa:table blue');
-
-
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
-
- parent::add($container);
- }
- }
- <?php
- /**
- * PerdcompList Listing
- * @author <your name here>
- */
- class PerdcompList extends TStandardList
- {
- protected $form; // registration form
- protected $datagrid; // listing
- protected $pageNavigation;
- protected $formgrid;
- protected $deleteButton;
- protected $transformCallback;
-
- /**
- * Page constructor
- */
- public function __construct()
- {
- parent::__construct();
-
- parent::setDatabase('perdcomp'); // defines the database
- parent::setActiveRecord('Perdcomp'); // defines the active record
- parent::setDefaultOrder('perdcomp_id', 'asc'); // defines the default order
- // parent::setCriteria($criteria) // define a standard filter
- parent::addFilterField('DataCriacao', 'like', 'DataCriacao'); // filterField, operator, formField
- parent::addFilterField('DataTransmissao', 'like', 'DataTransmissao'); // filterField, operator, formField
- parent::addFilterField('Nr_PERDCOMP', 'like', 'Nr_PERDCOMP'); // filterField, operator, formField
- parent::addFilterField('TipoDocumento_id', 'like', 'TipoDocumento_id'); // filterField, operator, formField
- parent::addFilterField('TipoCredito_id', 'like', 'TipoCredito_id'); // filterField, operator, formField
-
- // creates the form
- $this->form = new TQuickForm('form_search_Perdcomp');
- $this->form->class = 'tform'; // change CSS class
-
- $this->form->style = 'display: table;width:100%'; // change style
- $this->form->setFormTitle('Perdcomp');
-
- // create the form fields
- $DataCriacao = new TDate('DataCriacao');
- $DataTransmissao = new TDate('DataTransmissao');
- $Nr_PERDCOMP = new TEntry('Nr_PERDCOMP');
- // $TipoDocumento_id = new TSeekButton('TipoDocumento_id');
- $TipoDocumento_id = new ">TDBSeekButton('TipoDocumento_id', 'perdcomp', $this->form->getName(), 'TipoDocumento', 'TipoDocumentoDescricao', 'TipoDocumento_id', 'TipoDocumentoDescricao');
- $TipoDocumentoDescricao = new TEntry('TipoDocumentoDescricao');
- // $TipoCredito_id = new TSeekButton('TipoCredito_id');
- $TipoCredito_id = new ">TDBSeekButton('TipoCredito_id', 'perdcomp', $this->form->getName(), 'TipoCredito', 'TipoCreditoDescricao', 'TipoCredito_id', 'TipoCreditoDescricao');
- $TipoCreditoDescricao = new TEntry('TipoCreditoDescricao');
-
- $TipoDocumentoDescricao->setEditable(FALSE);
- $TipoDocumento_id->setSize('40');
- $TipoDocumentoDescricao->setSize('500');
- $TipoCreditoDescricao->setEditable(FALSE);
- $TipoCredito_id->setSize('40');
- $TipoCreditoDescricao->setSize('500');
-
- // add the fields
- $this->form->addQuickField('Data Criação', $DataCriacao, 80 );
- $this->form->addQuickField('Data Transmissão', $DataTransmissao, 80 );
- $this->form->addQuickField('Nº Perdcomp', $Nr_PERDCOMP, 200 );
- $this->form->addQuickFields('Tipo Documento:', [ $TipoDocumento_id, $TipoDocumentoDescricao ] );
- $this->form->addQuickFields('Tipo Crédito:', [ $TipoCredito_id, $TipoCreditoDescricao] );
-
-
- // keep the form filled during navigation with session data
- $this->form->setData( TSession::getValue('Perdcomp_filter_data') );
-
- // add the search form actions
- $this->form->addQuickAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
- $this->form->addQuickAction(_t('New'), new TAction(array('PerdcompForm', 'onEdit')), 'bs:plus-sign green');
-
- // creates a DataGrid
- $this->datagrid = new TDataGrid;
-
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->datatable = 'true';
- // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
-
- // creates the datagrid columns
- $column_DataCriacao = new TDataGridColumn('DataCriacao', 'Data Criação', 'left');
- $column_DataTransmissao = new TDataGridColumn('DataTransmissao', 'Data Transmissão', 'left');
- $column_Nr_PERDCOMP = new TDataGridColumn('Nr_PERDCOMP', 'Nº Perdcomp', 'left');
- $column_TipoDocumentoDescricao = new TDataGridColumn('tipo_documento->TipoDocumentoDescricao', 'Tipo Documento', 'left');
- $column_TipoCreditoDescricao = new TDataGridColumn('tipo_credito->TipoCreditoDescricao', 'Tipo Crédito','left');
-
- $column_GED = new TDataGridColumn('GED', 'Ged', 'left');
- // add the columns to the DataGrid
- $this->datagrid->addColumn($column_DataCriacao);
- $this->datagrid->addColumn($column_DataTransmissao);
- $this->datagrid->addColumn($column_Nr_PERDCOMP);
- $this->datagrid->addColumn($column_TipoDocumentoDescricao);
- $this->datagrid->addColumn($column_TipoCreditoDescricao);
- $this->datagrid->addColumn($column_GED);
- // creates the datagrid column actions
- $order_DataCriacao = new TAction(array($this, 'onReload'));
- $order_DataCriacao->setParameter('order', 'DataCriacao');
- $column_DataCriacao->setAction($order_DataCriacao);
-
- $order_DataTransmissao = new TAction(array($this, 'onReload'));
- $order_DataTransmissao->setParameter('order', 'DataTransmissao');
- $column_DataTransmissao->setAction($order_DataTransmissao);
-
- $order_Nr_PERDCOMP = new TAction(array($this, 'onReload'));
- $order_Nr_PERDCOMP->setParameter('order', 'Nr_PERDCOMP');
- $column_Nr_PERDCOMP->setAction($order_Nr_PERDCOMP);
-
- // inline editing
- $DataCriacao_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $DataCriacao_edit->setField('perdcomp_id');
- $column_DataCriacao->setEditAction($DataCriacao_edit);
-
- $DataTransmissao_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $DataTransmissao_edit->setField('perdcomp_id');
- $column_DataTransmissao->setEditAction($DataTransmissao_edit);
-
- $Nr_PERDCOMP_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $Nr_PERDCOMP_edit->setField('perdcomp_id');
- $column_Nr_PERDCOMP->setEditAction($Nr_PERDCOMP_edit);
-
- $GED_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $GED_edit->setField('perdcomp_id');
- $column_GED->setEditAction($GED_edit);
-
- // define the transformer method over image
- $column_DataCriacao->setTransformer( function($value, $object, $row) {
- $date = new DateTime($value);
- return $date->format('d/m/Y');
- });
- // define the transformer method over image
- $column_DataTransmissao->setTransformer( function($value, $object, $row) {
- $date = new DateTime($value);
- return $date->format('d/m/Y');
- });
- // define the transformer method over image
- $column_GED->setTransformer( function($value, $object, $row) {
- if (file_exists($value)) {
- return new TImage($value);
- }
- });
-
- // create EDIT action
- $action_edit = new TDataGridAction(array('PerdcompForm', 'onEdit'));
- // $action_edit->setUseButton(TRUE);
- $action_edit->setButtonClass('btn btn-default');
- $action_edit->setLabel(_t('Edit'));
- $action_edit->setImage('fa:pencil-square-o blue fa-lg');
- $action_edit->setField('perdcomp_id');
- $this->datagrid->addAction($action_edit);
-
- // create DELETE action
- $action_del = new TDataGridAction(array($this, 'onDelete'));
- // $action_del->setUseButton(TRUE);
- $action_del->setButtonClass('btn btn-default');
- $action_del->setLabel(_t('Delete'));
- $action_del->setImage('fa:trash-o red fa-lg');
- $action_del->setField('perdcomp_id');
- $this->datagrid->addAction($action_del);
-
- // create the datagrid model
- $this->datagrid->createModel();
-
- // create the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
-
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- $container->add($this->datagrid);
- $container->add($this->pageNavigation);
-
- parent::add($container);
- }
-
- }
Veja abaixo um trecho do código com relacionamento
Neste trecho o atributo na tabela deve ser o mesmo nome do atributo do relacionamento: $this->cliente = new Cliente($this->cliente_id);
$this->cliente_id = refere-se ao atributo "cliente_id" da tabela relacionada
Neste trecho, fazemos referência ao $object->id qe é "clientes", com $this->cliente_id da tabela relacionada :$this->cliente_id = $object->id;
O atributo da tabela relacionada está com o mesmo nome?
<php
/**
* Method set_cliente
* Sample of usage: $backlog->cliente = $object;
* @param $object Instance of Cliente
*/
public function set_cliente(Cliente $object)
{
$this->cliente = $object;
$this->cliente_id = $object->id;
}
/**
* Method get_cliente
* Sample of usage: $backlog->cliente->attribute;
* @returns Cliente instance
*/
public function get_cliente()
{
// loads the associated object
if (empty($this->cliente))
$this->cliente = new Cliente($this->cliente_id);
// returns the associated object
return $this->cliente;
}
>
Respondendo as demais perguntas:
Limpar campos = normalmente ao inserir um registro, ou mantemos o registro na tela com o ID gerado para edição, ou redirecionamos para um lista. Se deseja voltar a tela como novo registro, ao salvar vc pode simplesmente não utilizar: $this->form->setData($dados);
Imgens: vc diz fazer upload?
Problema resolvido.
Muito obrigado Marcelo.