Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Metodo onDelete parâmetro $key = Undefined index: key Boa noite a todos, tenho um formulário de cadastro e implementei um botão de excluir, porém na action delete não está chegando o parâmetro $key, gostaria de saber onde errei, utilizando o var_dump verifiquei que na linha 202 o parâmetro desaparece, Obrigado ...
MC
Metodo onDelete parâmetro $key = Undefined index: key  
Boa noite a todos, tenho um formulário de cadastro e implementei um botão de excluir, porém na action delete não está chegando o parâmetro $key, gostaria de saber onde errei, utilizando o var_dump verifiquei que na linha 202 o parâmetro desaparece, Obrigado

  1. <?php
  1. <?php
  2. /**
  3.  * Imagem_Marca do gadoForm
  4.  * @author  <your name here>
  5.  */
  6. class Imagem_MarcagadoForm extends TPage
  7. {
  8.     protected $form// form
  9.     
  10.     /**
  11.      * Form constructor
  12.      * @param $param Request
  13.      */
  14.     public function __construct$param )
  15.     {
  16.         parent::__construct();
  17.         
  18.         
  19.         // creates the form
  20.         $this->form = new TQuickForm('form_Imagem_Marcagado');
  21.         $this->form->class 'tform'// change CSS class
  22.         $this->form = new BootstrapFormWrapper($this->form);
  23.         $this->form->style 'display: table;width:100%'// change style
  24.         
  25.         // define the form title
  26.         $this->form->setFormTitle('Imagem_Marcagado');
  27.         
  28.         // create the form fields
  29.         $id = new TEntry('id');       
  30.         $nome = new TSlim('nome');
  31.         $iniciais = new TEntry('iniciais');
  32.         $propriedade_id = new TEntry('propriedade_id');
  33.         
  34.         $nome->setSize'100%'100);
  35.         $nome->setDataProperties(['label'=>'Upload marcador']);
  36.         // add the fields
  37.         $this->form->addQuickField('Id'$id,  '100%' );
  38.         $this->form->addQuickField('Nome'$nome,  '100%' );
  39.         $this->form->addQuickField('Iniciais'$iniciais,  '100%' , new TRequiredValidator);
  40.         $this->form->addQuickField('Propriedade'$propriedade_id,  '50%' );
  41.         if (!empty($id))
  42.         {
  43.            $id->setEditable(FALSE);
  44.         }
  45.         if (!empty($propriedade_id))
  46.         {
  47.            $propriedade_id->setEditable(FALSE);
  48.         }
  49.        
  50.          
  51.         // create the form actions
  52.         $btn  $this->form->addQuickAction(_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o');
  53.         $btn2  $this->form->addQuickAction(_t('Delete'), new TAction(array($this'onDelete')), 'fa:trash-o red fa-lg');
  54.         $btn->class 'btn btn-sm btn-primary';
  55.         
  56.         $this->form->addQuickAction('Voltar',  new TAction(['PropriedadeForm''onEdit'] ,['key'=>TSession::getValue('propriedade')]), 'fa:table blue');
  57.         
  58.        
  59.         // vertical box container
  60.         $container = new TVBox;
  61.         $container->style 'width: 90%';
  62.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  63.         $container->add(TPanelGroup::pack('Title'$this->form));
  64.         
  65.         parent::add($container);
  66.     }
  67.     /**
  68.      * Save form data
  69.      * @param $param Request
  70.      */
  71.     public function onSave$param )
  72.     {
  73.         try
  74.         {
  75.             TTransaction::open('permission'); // open a transaction
  76.             
  77.             /**
  78.             // Enable Debug logger for SQL operations inside the transaction
  79.             TTransaction::setLogger(new TLoggerSTD); // standard output
  80.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  81.             **/
  82.             
  83.             $this->form->validate(); // validate form data
  84.  
  85.             $object = new Imagem_Marcagado;       
  86.             $data $this->form->getData(); // get form data as array 
  87.             $object->fromArray( (array) $data); // load the object with data
  88.                   
  89.            
  90.             // Get posted data
  91.             $imagem Slim::getImages();
  92.             $propriedade TSession::getValue('propriedade');
  93.             
  94.             // No image found under the supplied input name
  95.             if ($imagem)
  96.             {            
  97.                 $imagem $imagem[0];
  98.                 // save output data if set
  99.                 if (isset($imagem['output']['data']))
  100.                 {
  101.                     // Save the file
  102.                     $name $imagem['output']['name'];
  103.         
  104.                     // We'll use the output crop data
  105.                     $data $imagem['output']['data'];
  106.         
  107.                     $output Slim::saveFile($data$name'images/marcagado/'false);
  108.                     $object->nome $output['path'];
  109.                 }
  110.             }
  111.                         
  112.             $object->propriedade_id $propriedade;   
  113.              if (empty($object->nome))
  114.             {
  115.                 throw new Exception('Campo nome é obrigatório');
  116.             }           
  117.             
  118.             $object->store(); // save the object
  119.             
  120.             // get the generated id
  121.             //$data->id = $object->id;
  122.             
  123.             $this->form->setData($data); // fill form data
  124.             TTransaction::close(); // close the transaction
  125.             
  126.             //new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  127.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'), new TAction(['PropriedadeForm''onEdit'] ,['key'=>TSession::getValue('propriedade')]) );
  128.         }
  129.         catch (Exception $e// in case of exception
  130.         {
  131.             new TMessage('error'$e->getMessage()); // shows the exception error message
  132.             $this->form->setData$this->form->getData() ); // keep form data
  133.             TTransaction::rollback(); // undo all pending operations
  134.         }
  135.     }
  136.     
  137.     /**
  138.      * Clear form data
  139.      * @param $param Request
  140.      */
  141.     public function onClear$param )
  142.     {
  143.         $this->form->clear(TRUE);
  144.     }
  145.     
  146.     /**
  147.      * Load object to form data
  148.      * @param $param Request
  149.      */
  150.     public function onEdit$param )
  151.     {
  152.         try
  153.         {
  154.             if (isset($param['key']))
  155.             {
  156.                 $key $param['key'];  // get the parameter $key
  157.                 TTransaction::open('permission'); // open a transaction
  158.                 $object = new Imagem_Marcagado($key); // instantiates the Active Record
  159.                 $this->form->setData($object); // fill the form
  160.                 TTransaction::close(); // close the transaction
  161.             }
  162.             else
  163.             {
  164.                 $this->form->clear(TRUE);
  165.             }
  166.         }
  167.         catch (Exception $e// in case of exception
  168.         {
  169.             new TMessage('error'$e->getMessage()); // shows the exception error message
  170.             TTransaction::rollback(); // undo all pending operations
  171.         }
  172.     }
  173.     public function onDelete($param)
  174.     {
  175.        // define the delete action
  176.         $action = new TAction(array($this'Delete'));
  177.         $action->setParameters($param); // pass the key parameter ahead
  178.         
  179.         // shows a dialog to the user
  180.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  181.         
  182.     }
  183.     /**
  184.      * Delete a record
  185.      */
  186.     public function Delete($param)
  187.     {
  188.         try
  189.         {
  190.             //$key = $param['key']; // get the parameter $key
  191.             TTransaction::open('permission'); // open a transaction with database
  192.             $object = new Imagem_Marcagado($keyFALSE); // instantiates the Active Record  
  193.             
  194.             $object->delete(); // deletes the object from the database
  195.             TTransaction::close(); // close the transaction            
  196.             new TMessage('info'TAdiantiCoreTranslator::translate('Record deleted')); // success message
  197.         }
  198.         catch (Exception $e// in case of exception
  199.         {
  200.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  201.             TTransaction::rollback(); // undo all pending operations
  202.         }
  203.     }
  204. }
  205. ?>

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (3)


NR

A linha onde a variável $key é atribuída está comentada:
  1. <?php
  2. // linha 200
  3. //$key = $param['key']; // get the parameter $key
  4. ?>
MC

Obrigado pela ajuda, descomentei a linha 200 e como havia falado anteriormente o valor não chega no parâmetro, ainda continua apresentando erro e o registro não é apagado.

Notice: Undefined index: key in C:wampwwwpatrulharural2appcontrolpatrulhaincludesImagem_MarcagadoForm.class.php on line 200

  1. <?php
  2.  public function Delete($param)
  3.     {
  4.         try
  5.         {
  6.             $key $param['key']; // get the parameter $key
  7.             TTransaction::open('permission'); // open a transaction with database
  8.             $object = new Imagem_Marcagado($keyFALSE); // instantiates the Active Record  
  9.             
  10.             $object->delete(); // deletes the object from the database
  11.             TTransaction::close(); // close the transaction            
  12.             new TMessage('info'TAdiantiCoreTranslator::translate('Record deleted')); // success message
  13.  //new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'), new TAction(['PropriedadeForm', 'onEdit'] ,['key'=>TSession::getValue('propriedade')]) );
  14.         }
  15.         catch (Exception $e// in case of exception
  16.         {
  17.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  18.             TTransaction::rollback(); // undo all pending operations
  19.         }
  20.     }
  21. ?>


Se substituo a linha 202 e mudo o parâmetro $key para $param['id'] conforme abaixo, o registro é apagado.

$object = new Imagem_Marcagado($param['id'], FALSE)

Obrigado
NR

Agora que vi que você está chamando a função Delete através de um botão no formulário. Acontece que a chave "key" é usada pelas ações da datagrid. Ao usar um botão em um formulário essa chave não é utilizada e a variável $param vai conter os dados dos campos do formulário.