Insert_batch or update_batch - CodeIgniter

1

Hello! Can you help me with an insert_batch or update_batch.

Through the code below I need to insert, update, or delete an item from the database.

The way it is I am able to insert, but my problem is when I will update, because when it is to update, updates only the last id, while other data (id) that also underwent changes in the form, are not updated in the database of data.

The object contains a crud field.

If this field is given the letter C , then I have to insert an item in the database
If it comes R , I do not do anything. If U , then update the field,
If it comes, D , then I delete the database item.

See the object of $endereco = $objeto_decode->enderecos;

[0] => stdClass Object
    (
        [id] => 1
        [cep] => 30810-350
        [logradouro] => Rua Flor da Laranjeira
        [numero] => 71
        [complemento] => 
        [bairro] => Jardim Alvorada
        [cidade] => Belo Horizonte
        [uf] => MG
        [observacao] => Teste
        [crud] => R
        [devedor_id] => 1
        [id_origem_dados] => 1
    )

[1] => stdClass Object
    (
        [id] => 3
        [cep] => 32667-538
        [logradouro] => Rua Jhenifer Naiara Aguiar
        [numero] => 29
        [complemento] => 
        [bairro] => Paulo Camilo
        [cidade] => Betim
        [uf] => MG
        [observacao] => GGG
        [crud] => R
        [devedor_id] => 1
        [id_origem_dados] => 1
    )

[2] => stdClass Object
    (
        [id] => 4
        [cep] => 32667-538
        [logradouro] => Rua Jhenifer Naiara Aguiar
        [numero] => 29
        [complemento] => CS
        [bairro] => Paulo Camilo
        [cidade] => Betim
        [uf] => MG
        [observacao] => 
        [crud] => R
        [devedor_id] => 1
        [id_origem_dados] => 1
    )

[3] => stdClass Object
    (
        [id] => 5
        [cep] => 32667-538
        [logradouro] => Rua Jhenifer Naiara Aguiar
        [numero] => 29
        [complemento] => CS
        [bairro] => Paulo Camilo
        [cidade] => Betim
        [uf] => MG
        [observacao] => AAAATTT
        [crud] => R
        [devedor_id] => 1
        [id_origem_dados] => 1
    )
)

So I'm doing the following:

$array =  (array) $endereco;
foreach ($endereco as $endereco)
 {
    $endereco_id           = $endereco->id;            
    $endereco_cep          = $endereco->cep;
    $endereco_logradouro   = $endereco->logradouro;
    $endereco_numero       = $endereco->numero;
    $endereco_complemento  = $endereco->complemento;
    $endereco_bairro       = $endereco->bairro;
    $endereco_cidade       = $endereco->cidade;
    $endereco_uf           = $endereco->uf;
    $endereco_observacao   = $endereco->observacao;
    $endereco_crud         = $endereco->crud;
    $endereco_devedor_id   = $endereco->devedor_id;
    $endereco_origem_dados = $endereco->id_origem_dados;
    //echo $endereco->crud);
 }
        
 $batch[] = array(
    'id'              => $endereco_id,
    'cep'             => $endereco_cep,
    'logradouro'      => $endereco_logradouro,
    'numero'          => $endereco_numero,
    'complemento'     => $endereco_complemento,
    'bairro'          => $endereco_bairro,
    'cidade'          => $endereco_cidade,
    'uf'              => $endereco_uf,
    'observacao'      => $endereco_observacao,
    'devedor_id'      => $endereco_devedor_id,
    'id_origem_dados' => $endereco_origem_dados,
    );
    
    if ($endereco_id == 0 && $endereco_crud == "C")
		{
		    $this->db->insert_batch('tbl_devedor_endereco', $batch, 'id');
		} 
		elseif ($endereco_id != 0 && $endereco_crud == "R")
		{
		    echo ($endereco_id.' retrive'.'<br>');
		} 
		elseif ($endereco_id != 0 && $endereco_crud == "U")
		{
		    $this->db->update_batch('tbl_devedor_endereco', $batch, 'id');
		} 
		elseif ($endereco_id != 0 && $endereco_crud == "D")
		{
		   echo ($endereco_id.' delete'.'<br>');
		}
    
asked by anonymous 30.08.2017 / 18:21

1 answer

1

This (only the last item is processed) happens because the key that closes foreach is in the wrong place it should be at the end. Since you need to check what to do with each record it does not make much sense to use insert_batch() . Delete those assignments that are left over.

$array =  (array) $endereco;
foreach ($endereco as $e){
   $batch[] = array(
            'id'              => $e->id,
            'cep'             => $e->cep,
            'logradouro'      => $e->logradouro,
            'numero'          => $e->numero,
            'complemento'     => $e->complemento,
            'bairro'          => $e->bairro,
            'cidade'          => $e->cidade,
            'uf'              => $e->uf,
            'observacao'      => $e->observacao,
            'devedor_id'      => $e->devedor_id,
            'id_origem_dados' => $e->origem_dados,
        );

    if ($e->id == 0 && $e->crud == "C"){
            $this->db->insert_batch('tbl_devedor_endereco', $batch, 'id');
        }elseif ($e->id!= 0 && $e->crud == "R"){
            echo ($e->id.' retrive'.'<br>');
        }elseif ($e->id != 0 && $e->crud == "U"){
            $this->db->update_batch('tbl_devedor_endereco', $batch, 'id');
        }elseif ($e->id != 0 && $e->crud == "D"){
           echo ($e->id.' delete'.'<br>');
        }
}// FIM DO FOREACH      
    
30.08.2017 / 20:59