Doubt in the logic of registration with codeigniter!

Asked

Viewed 381 times

-2

I have a "table" with the following fields: person, tree, date start (Default), datafim (Default),

My doubt in logic is as follows:

Register an employee and his area of responsibility with the start date. When re-registering with a new employee referring to the area that already has a responsible, automatically is filled by default the end date of the former responsible and create a new start date with the new responsible for that area without the end date. In fact, I’m trying to do a background on the employees responsible for certain areas, with their start and end date.

How should I proceed in logic?

public function cadastrar(){
    esta_logado();
    $this->form_validation->set_rules('pessoa', 'FUNCIÓANARIO', 'trim|required');
    $this->form_validation->set_rules('area', 'ÁREA', 'trim|required');

    if($this->form_validation->run() == TRUE):
    $dados['pessoa_id'] = $this->input->post('pessoa', TRUE);
    $dados['area_id'] = $this->input->post('area', TRUE);
    $this->responsavel->do_insert($dados);

    endif;


    set_tema('titulo', 'Cadastrar responsável');
    set_tema('conteudo', load_modulo('responsavel', 'cadastrar'));
    load_template();    
}

The logic should be done in this controller! this function is registering in the database!

2 answers

0

Do you want the new registered user to receive the start date (but not the end date), and the end date of the last user entered in the table, receive the equivalent date that would be the start date of the new user? OK.

If so, before you enter the new user in the database (table), you need to pick up and edit the last user inserted in your table, in my example, I will use the table called tb_users.

SELECT * FROM tb_users WHERE id = (SELECT MAX(id) FROM tb_users); // essa é a query.

NOTE: The table must have an ID single increasing, otherwise several rows will be returned in the query and will not do what you want. Probably your table is in ascending order of ID, it would be more logical for your problem, so would be all right so far.

After picking up the last user ID, you can then do a simple update on that line. I will present a pseudocode, since we do not know how your Inserts, reads, updates and Letes are made, in case you change the logic.

$ultimo_usuario = carregarUsuario(id_da_ultima_linha_da_tabela); // último inserido
date_default_timezone_set('America/Sao_Paulo'); // apenas setando a data
$ultimo_usuario->datadefim = date('Y-m-d H:i:s'); // data e hora atual da minha região
$ultimo_usuario->update(); // update, atualizar a linha no baco.

// novo usário, instancie e adicione os dados necessários. Só irei mostras a data
$novo_usuario = new Usuario();
$novo_usuario->datadeinicio = $ultimo_usuario->datadefim; // data de inicio do novo, é a data fim do antigo, certo?
$novo_usuario->save(); // insert, inserir linha no banco.

If you don’t own ID unique in the table, to query, you will need to save the information (id) of the last user entered, somewhere.

0

I’m not sure if you want a logic for Codeigniter or a function that can accomplish this.

If we think in database what you can do is a query returning the ID of the record that has this information, if there you perform an update in it setting the start date and then perform the insertion of the new record.

There is the possibility to accomplish this through a single Insert instruction, creating a Rigger that can be triggered to any Insert performed in the system for this table, updating the previous record with the date of termination of the action.

I see more an SQL statement to be performed than actually a method created in a specific class for this, where it might consume more system resources.

Pass more information, for example which bank you use, whether there may or may not be a responsible record in the system when entering a new one, etc. so I can try to create some code that helps you.

I hope to have helped and given an idea of how it can be done

Abs

  • The database is mysql, the logic would be for Codeigniter. I already have a record in the database, with the employee responsible for an area. When I register a new employee referring to some area that already has a registered responsible, be filled in the end date of the former responsible employee and create - if a new registration, with the new employee responsible for the area and the start date. By logic the end date of the former employee and the start date of the new employee. Excuse anything if my logic is not being clear!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.