Doubt in the logic of registration with codeigniter!

-2

I have a "table" with the following fields: person_id, area_id, default, datafim (Default),

My logic doubt is as follows:

Register an employee and their area of responsibility with the start date. When you register again with a new employee regarding the area that already has a person in charge, the default date is automatically filled by the previous person and create a new start date with the new person in that area without the end date. In fact, I'm trying to make a history of the employees responsible for certain areas, with their start and end dates.

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 at the bank!

    
asked by anonymous 14.11.2014 / 21:22

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 date equivalent to what would be the start of the new user? OK.

If this is, before you insert the new user into the database (table), you need to get and edit the last user entered in your table, in my example, I will use the table named tb_users .

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

NOTE: The table must have a unique unique ID , 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 everything would be fine here.

After getting the ID of the last user, 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 deletes are done, in case, you add 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 do not have a unique ID in the table, to do the query, you will need to save the id of the last user entered somewhere.

    
02.12.2014 / 13:34
0

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

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

There is the possibility of doing this through a single insert statement, creating a trigger that can be triggered to any insert made in the system for this table, updating the previous record with the end date of the action.

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

Pass more information, for example, which bank you use, whether or not there is a responsible person record in the system when inserting a new one, etc. so I can try to create some code that will help you.

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

Abs

    
14.11.2014 / 22:47