PA
Fatal error: Uncaught Error: Call to a member function getData()
Boa tarde senhores, estou enfrentando um problema ao chamar o formulário do onInputDialog, não sei como chamr o valor enviado.
Tentei $data2 = $this->form2->getData(); porém dá error
Fatal error: Uncaught Error: Call to a member function getData() on null in C:xampphtdocsacra5appcontroldadosAlertaEncomendaList.class.php:272 Stack trace: #0 [internal function]: AlertaEncomendaList->onConfirm1(Array) #1 C:xampphtdocsacra5libadianticontrolTPage.php(51): call_user_func(Array, Array) #2 C:xampphtdocsacra5libadianticontrolTPage.php(205): AdiantiControlTPage->run() #3 C:xampphtdocsacra5libadiantibaseAdiantiStandardListTrait.php(503): AdiantiControlTPage->show() #4 C:xampphtdocsacra5libadiantibaseAdiantiStandardListTrait.php(503): AdiantiBaseTStandardList->show() #5 C:xampphtdocsacra5libadianticoreAdiantiCoreApplication.php(64): AlertaEncomendaList->show(Array) #6 C:xampphtdocsacra5engine.php(23): AdiantiCoreAdiantiCoreApplication::run(true) #7 C:xampphtdocsacra5engine.php(65): TApplication::run(true) #8 {main} thrown in C:xampphtdocsacra5appcontroldadosAlertaEncomendaList.class.php on line 272
Segue abaixo o código:
Tentei $data2 = $this->form2->getData(); porém dá error
Fatal error: Uncaught Error: Call to a member function getData() on null in C:xampphtdocsacra5appcontroldadosAlertaEncomendaList.class.php:272 Stack trace: #0 [internal function]: AlertaEncomendaList->onConfirm1(Array) #1 C:xampphtdocsacra5libadianticontrolTPage.php(51): call_user_func(Array, Array) #2 C:xampphtdocsacra5libadianticontrolTPage.php(205): AdiantiControlTPage->run() #3 C:xampphtdocsacra5libadiantibaseAdiantiStandardListTrait.php(503): AdiantiControlTPage->show() #4 C:xampphtdocsacra5libadiantibaseAdiantiStandardListTrait.php(503): AdiantiBaseTStandardList->show() #5 C:xampphtdocsacra5libadianticoreAdiantiCoreApplication.php(64): AlertaEncomendaList->show(Array) #6 C:xampphtdocsacra5engine.php(23): AdiantiCoreAdiantiCoreApplication::run(true) #7 C:xampphtdocsacra5engine.php(65): TApplication::run(true) #8 {main} thrown in C:xampphtdocsacra5appcontroldadosAlertaEncomendaList.class.php on line 272
Segue abaixo o código:
- <?php
- class AlertaEncomendaList extends TStandardList
- {
- protected $form; // registration form
- protected $datagrid; // listing
- protected $pageNavigation;
- protected $estabelecimento;
- // trait with onReload, onSearch, onDelete...
- use Adianti\Base\AdiantiStandardListTrait;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- public function __construct()
- {
- parent::__construct();
- $this->setDatabase('permission'); // defines the database
- $this->setActiveRecord('Encomenda'); // defines the active record
- $this->addFilterField('name', 'like', 'name'); // filter field, operator, form field
- $this->setDefaultOrder('id', 'desc'); // define the default order
- $this->estabelecimento = TSession::getValue('userunitid');
- // creates the form
- $this->form = new BootstrapFormBuilder('form_lista_apartamentos');
- $this->form->setFormTitle('Alerta de encomendas');
- $criterio = new TCriteria;
- $criterio->add(new TFilter('fk_id_system_unit','=', $this->estabelecimento));
- $apartamento = new TDBUniqueSearch('fk_id_unidade', 'permission', 'Unidade', 'id', 'nome', 'nome', $criterio);
- $apartamento->setSize('85%');
- $this->form->addFields( [new TLabel('Apartamento')], [$apartamento] );
- // add form actions
- $this->form->addAction('Alerta de encomenda', new TAction([$this, 'onSave']), 'fa:plus white')->class = 'btn btn-sm btn-success';
- // keep the form filled with the search data
- $this->form->setData( TSession::getValue('StandardDataGridView_filter_data') );
- // creates the DataGrid
- $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->datagrid->width = "100%";
- // creates the datagrid columns
- $col_dt_chegada = new TDataGridColumn('data_hora_chegada', 'Data de chegada', 'center');
- $col_dt_entrega = new TDataGridColumn('data_hora_entrega', 'Data de entrega', 'center');
- $col_apartamento = new TDataGridColumn('fk_id_unidade', 'Apartamento', 'center');
- $col_status = new TDataGridColumn('fk_id_system_unit', 'Status', 'center');
- $this->datagrid->addColumn($col_dt_chegada);
- $this->datagrid->addColumn($col_dt_entrega);
- $this->datagrid->addColumn($col_apartamento);
- $this->datagrid->addColumn($col_status);
- $col_dt_chegada->setTransformer( function($value, $object, $row) {
- $data = TDate::date2br($value );
- return $data;
- });
- //Formata os campos da encomenda da coluna
- $col_status->setTransformer( function($value, $object, $row) {
- $class = ($value=='N') ? 'success' : 'danger';
- $label = ($value=='N') ? 'A caminho' : 'Na portaria';
- $div = new TElement('span');
- $div->class="label label-{$class}";
- $div->style="text-shadow:none; font-size:12px; font-weight:lighter";
- $div->add($label);
- return $div;
- });
- // create EDIT action
- $action_edit = new TDataGridAction(array($this, 'onInputDialog'));
- $action_edit->setLabel('Entrega');
- $action_edit->setImage('fa:envelope-o blue fa-lg');
- $action_edit->setField('id');
- $this->datagrid->addAction($action_edit);
- $col_apartamento->setTransformer( function($value, $object, $row) {
- $unidade = new Unidade($value);
- return $unidade->nome;
- });
- $col_dt_chegada->setAction( new TAction([$this, 'onReload']), ['order' => 'data_hora_entrega']);
- $col_apartamento->setAction( new TAction([$this, 'onReload']), ['order' => 'fk_id_system_unit']);
- $col_status->setAction( new TAction([$this, 'onReload']), ['order' => 'login']);
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // creates the page structure using a table
- $vbox = new TVBox;
- $vbox->style = 'width: 100%';
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- $vbox->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
- // add the table inside the page
- parent::add($vbox);
- }
- public function onSave(){
- try {
- TTransaction::open('permission');
- $data = $this->form->getData('Encomenda');
- $object = new Encomenda;
- $object->fk_id_system_unit = $this->estabelecimento;
- $object->fk_id_unidade = $data->fk_id_unidade;
- $object->data_hora_chegada = date('Y-m-d H:i:s');
- $object->store();
- new TMessage('info', 'Registro salvo com sucesso!');
- TTransaction::close();
- $this->onReload(); // recarregar a página
- } catch (Exception $e) {
- new TMessage('error', $e->getMessage());
- }
- }
- public function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- $repository = new TRepository('Encomenda');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'desc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- if (is_callable($this->transformCallback))
- {
- call_user_func($this->transformCallback, $objects, $param);
- }
- $this->datagrid->clear();
- if ($objects)
- {
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- if($object->fk_id_system_unit == $this->estabelecimento){
- // add the object inside the datagrid
- $this->datagrid->addItem($object);
- }
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count= $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /*public function btn_action($param){
- $form2 = new TQuickForm('input_form');
- $form2->style = 'padding:20px';
- $confirmar = new TCheckGroup('confirmar');
- $options = [1=>''];
- $confirmar->addItems($options);
- $confirmar->setLayout('horizontal');
- $form2->addQuickField('Confirmar entrega ', $confirmar);
- $key = $param['key'];
- echo $key;
- $form2->addQuickAction('Confirmar', new TAction(array($this, 'onConfirm1')), 'fa:save green');
- // show the input dialog
- new TInputDialog('Confirmar entrega ', $form2);
- }*/
- public function onInputDialog( $param )
- {
- TTransaction::open('permission');
- $confirmar = new TCheckGroup('confirm');
- //$amount = new TEntry('amount');
- $confirmar->setValue($param['key']);
- $options = [1=>''];
- $confirmar->addItems($options);
- $confirmar->setLayout('horizontal');
- $repository = new TRepository('Encomenda');
- $criteria = new TCriteria;
- $criteria->add(new TFilter('id','=', $param['key']));
- $objects = $repository->load($criteria, FALSE);
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- $form2 = new TForm('input_form');
- $form2->style = 'padding:20px';
- $table = new TTable;
- $table->addRowSet( new TLabel('Confirmar entrega '), $confirmar );
- //$table->addRowSet( $lbl = new TLabel('Amount: '), $amount );
- //$lbl->setFontColor('red');
- //$form2->setFields(array($confirmar, $amount));
- $form2->add($table);
- $casa = new Unidade($object->fk_id_unidade);
- $casa->nome;
- // show the input dialog
- $action = new TAction(array($this, 'onConfirm1'));
- $action->setParameter('stay-open', 1);
- new TInputDialog('Confirmar entrega em '.$casa->nome, $form2, $action, 'Confirmar');
- }
- TTransaction::close();
- }
- public function onConfirm1($param){
- try {
- TTransaction::open('permission');
- $data2 = $this->form2->getData();
- //if($data->confirm){
- $object_entrega = new Encomenda($param['key']);
- $object_entrega->data_hora_entrega = date('Y-m-d H:i:s');
- // }
- TTransaction::close();
- // recarregar a página
- //$this->onReload();
- } catch (Exception $e) {
- new TMessage('error', $e->getMessage());
- }
- }
- }
Ao criar uma variável dentro de uma função ela só será acessível dentro desta. Eu usaria as 2 funções(onInputDialog e onConfirm1) como estáticas e pegaria os dados do form através da variável $param.
Mais informações sobre escopo de variáveis:
https://www.php.net/manual/pt_BR/language.variables.scope.php