0
Hello!
I ask this question in order to remove a doubt I have, and to be able to read other people’s ideas, of how I can insert or delete an item, to a certain operador_id
.
As the first image shows, when loading a registration form operadores
, I list all the unidades
registered in the system.
So I select uma
or mais
unit, whose operador
should belong.
And by clicking save, the objeto operador
, that is marked red will be sent as following image:
Where that which is selected from green is (are) a(s) unit(s), of which the operador
public.
See the database structure in the following image.
My doubt begins here.
If it is for inclusion, I can use the method, as in the example below:
public function incluir_unidade($dados, $operador_id)
{
$array = (array) $dados;
foreach ($dados as $uni)
{
$unidade = [
'ope_id' => $operador_id,
'uni_id' => $uni->unidade_id,
];
$this->db->insert($this->tb_operador_unidade, $unidade);
}
}
But I wanted to understand, how can I do in case of update:
To insert, delete unidade(s)
at the operador
.
Taking into account the tables below:
Unidade A
exists in the array, but already exists in the database (in this case it does nothing)Unidade A
exists in the array and does not exist in the database (in this case, it includes)Unidade A
does not exist in the array, but exists in the database (in this case, delete from the database)Unidade A
does not exist in the array and does not exist in the database (in this case, does nothing).
This example is just what I imagine, if there’s another way to do it, I’d like to know..
Wagner, in case of update, I delete all items belonging to this user, and re-host, based on the submitted array.
– Sr. André Baill