Insert_batch or update_batch - Codeigniter

Asked

Viewed 245 times

1

Hello!
Can 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.

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

The object contains a field crud.

If this field receives the letter C, then I must insert an item into the database
If you come R, do nothing.
If you come U, then update the field,
If you come, D, then delete the database item.

See the object
$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 here’s what I’m doing:

$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>');
		}

1 answer

1


This (only the last item is processed) happens because the key that closes the foreach is in the wrong place it should be at the end. As you need to check what to do with each record it doesn’t make much sense to use insert_batch(). Eliminate those remaining assignments.

$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      

Browser other questions tagged

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