Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to perform a conditional update using Fatfree framework's ORM

The situation consists of attempting to use F3 ORM (MySQL) with a state machine. A transaction/action should only be performed/persisted to the database if the original status allows for the transition to occur.

The code below loads the object, applies the new state, creates a new transaction, and attempts to ensure that the state in the database is as expected by updating the record to the new state.

$instance = $f3->get('domainObject')->load(['id=?', $id]);

$stateMachine = new StateMachine($instance, $config);
$originalState = $instance->stateA;
  
$stateMachine->apply($body['state']);

$db->begin();
$db->exec(
  'UPDATE domainObject SET stateA=? WHERE stateA=?',
  $instance->stateA,
  $originalState
);
$instance->save();
$db->commit();

Even though the update and the save is within the same transaction the modification occurs even if the update fails to update the record (say when the stateA has changed).

How can I use F3 ORM to conditionally update the model with a clause that states stateA should be a certain value?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Assuming that $instance is an instance of DB\SQL\Mapper and that stateMachine->apply($body['state']) modifies $instance->stateA, then it should be as simple as:

$instance = $f3->get('domainObject')->load(['id=?', $id]);

$stateMachine = new StateMachine($instance, $config);

$instance->save();

The save() method triggers an UPDATE query, only if a field has been changed.

NB: be careful that the load() method may return FALSE. Your code should handle that case.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda