CS
Buscar dado no banco antes de salvar
Pessoal, para calcular um saldo, antes de salvar um dado, preciso ir ao banco e pegar a leitura anterior. Estou tentando utilizar no onSave, mas não esta atribuindo o valor ao campo. Meu código:
public function onSave( $param )
{
try
{
TTransaction::open('abastecimento'); // 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
$object = new OprLeitura; // create an empty object
$data = $this->form->getData(); // get form data as array
$object->fromArray( (array) $data); // load the object with data
$conn = TTransaction::get();
$ant = $conn->query("select max(leitura) from leitura where id_cad =".$object->id_cad);
$item_ant = new StdClass;
foreach ($ant as $row)
{
$item_ant->leitura = $row['leitura'];
}
$data->leitura_ant = $item_ant->leitura;
$this->form->setData( $data );
$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', TAdiantiCoreTranslator::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
}
}
public function onSave( $param )
{
try
{
TTransaction::open('abastecimento'); // 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
$object = new OprLeitura; // create an empty object
$data = $this->form->getData(); // get form data as array
$object->fromArray( (array) $data); // load the object with data
$conn = TTransaction::get();
$ant = $conn->query("select max(leitura) from leitura where id_cad =".$object->id_cad);
$item_ant = new StdClass;
foreach ($ant as $row)
{
$item_ant->leitura = $row['leitura'];
}
$data->leitura_ant = $item_ant->leitura;
$this->form->setData( $data );
$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', TAdiantiCoreTranslator::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
}
}
Quando você obtém os dados do formulário ($data = $this->form->getData()), o cad_id já existe?
Opa, existe sim, amigo. É que preciso para fazer um calculo de saldo, aí preciso pegar a leitura anterior do cliente. Resolvi assim:
Ok,
Pra você obter a leitura por um "nome", você precisaria utilizar "alias", por exemplo: $ant = $conn->query('select max(leitura) as leitura from leitura where id_cad ='.$object->id_cad);