Obtain Multidimensional Array (codeigniter) data?

Asked

Viewed 316 times

1

I had a case solved through the question of this link here in the O.R.:

Following the example of the above question, I have the following table tbl_devedor_contrato, with the following fields:

`id`
`plano`
`contrato`
`dt_contrato`
`dt_expiracao`
`crud`
`assessoria_id`
`empresa_id`
`importacao_id`

On the return of the object, I need to add an item to this object. This item will be called view, where to retrieve data from another table tbl_devedor_negociacao.

The output would be something like this:

`id = 1`
`plano = 10`
`contrato = 1`
`dt_contrato = 2017-09-08`
`dt_expiracao = 9999-09-08`
`crud = C`
`assessoria_id = 1`
`empresa_id = 1`
`importacao_id = 0`
`view = id = 1
       dt_negociacao = 1`
       atualizar = 1`
       id_finalizacao = 1`
       contrato_id = 1`
       crud = 1`

Turns out this item view, does not exist in the table tbl_devedor_contrato ?

So how can I create this item, since it does not exist in the table and assign values to it.

Below I have the relationship I need to do:

inserir a descrição da imagem aqui

Model:

//Obter devedor por ID | Contrato
public function obter_por_id_contrato($id)
{
    $this->db->from('tbl_devedor_contrato');
    $this->db->where('devedor_id',$id);
    $this->db->select("tbl_devedor_contrato.*, IF(tbl_devedor_contrato.crud = 'C', 'R', 'C') as crud", FALSE);
    $query = $this->db->get();
    return $query->result_array();
}`
  • You want to insert a column named view into the table tbl_devedor_email and within that column put data from another table, that’s it ?

  • @Leandrolima, eh this, but recover the way it is in the other question.

  • The link to another question is broken. I venture to say that what you want to do is bad practice and that there must be another, easier way to solve it. However I can not guide what would be the best way. You know how to relationship tables (?) This is not what you are looking for ?

  • @Leandrolima, updated Link. I know how to make relationships of tables, but I believe this is not the case.

  • see this image, to give you an idea. https://uploaddeimagens.com.br/imagens/img-png-129

    1. will need to create this field to return a data from another table, 2) as you will recover the data from these tables logs what will be the parameter for such recovery? and 3) which fields of the table logs will be related and recovered to display that field view that in the primary understanding it does not need to be only virtual physics (i.e., it does not need to exist, but, to be mounted equally in the example of the other answer). Put all this into your question and to edit see link creation and editing questions and answers
    1. I don’t need to create this field. 2) If there was a field created, it would be by INNER JOIN. 3) This field really doesn’t need to be physical, only virtual.
  • I placed the table of email as an example, but this view, will be applied in the table contrato, and this view will display the name of the company, whose field ID is present in the contract table. empresa_id. Take this JSON example and you will see an example: https://pastebin.com/embed_js/VMf30K5

  • @Virgilionovic, can you help me create this view? creating this, I can already manage with the rest.

  • Wagner yes but do the following put the two tables involved + relation + fields obtained there I promote an ok answer

  • @Virgilionovic did an update on the question, can see if it helps ?

  • Your doubt is because the contract result is a list and then Voce needs to interact in this list and attach the items related to trading?

  • @Virgilionovic, exactly. As in negotiations, I need to integrate the plots.. i need to get the contract negotiations and respectively the splits of negotiations

  • As it is, only displayed in the result, the real fields of tbl_debtor contract. So before creating the complete object, I need to include the view item, to know what are the negotiations of this contract and respectively the installments

  • Did it work ?? Wagner?

Show 10 more comments

1 answer

2


The work is a bit confusing at first, but, rewarding when you have an expected result, you need to use optimized mechanisms so that this assembly does not get low performance and does not access too much the database, with 3 and functions of PHP (array_filter), can have the expected result, I will propose a minimal example that can and should be reflected in your question upon the first reply:

Diagram of the database

inserir a descrição da imagem aqui

these tables have 1-to-many relationships and need to be joined together to generate a with all the information of a particular devedor ($devedor_id).

Classes

Debtor Contract

<?php

class Devedorcontrato_model extends CI_Model
{

    public function obter_contrato_devedor_id($id)
    {
        $this->db->from('tbl_devedor_contrato');
        $this->db->where('devedor_id',$id);
        $this->db->order_by('id');
        $query = $this->db->get();
        return $query->result_array();
    }

}

Devedor Negociações

<?php

class Devedornegociacao_model extends CI_Model
{

    public function obter_negociacao_devedor_id($id)
    {

        $this->db->from('tbl_devedor_negociacao');
        if (is_array($id))
        {
            $this->db->where_in('contrato_id',$id);
        }
        else
        {
            $this->db->where('contrato_id',$id);
        }
        $this->db->order_by('contrato_id');
        $query = $this->db->get();
        return $query->result_array();
    }

}

Debtor Installments

<?php

class Devedorparcelas_model extends CI_Model
{

    public function obter_parcelas_negociacao_id($id)
    {
        $this->db->from('tbl_devedor_parcela');
        if (is_array($id))
        {
            $this->db->where_in('negociacao_id',$id);
        }
        else 
        {
            $this->db->where('negociacao_id',$id);  
        }

        $this->db->order_by('negociacao_id');
        $query = $this->db->get();
        return $query->result_array();
    }

}

The class that will be responsible for managing all this information at once:

<?php

    class Devedordados_model extends CI_Model 
    {
        private $CI;

        public function __construct()
        {
            parent::__construct();
            $this->CI =& get_instance();
            $this->CI->load->model("devedor_model");
            $this->CI->load->model("devedoremail_model");
            $this->CI->load->model("devedorcontrato_model");
            $this->CI->load->model("devedornegociacao_model");
            $this->CI->load->model("devedorparcelas_model");
        }

        public function obter_todos_dados_por_id($id)
        {
            $result = array();

            //Contratos
            $result['contratos'] = $this->devedorcontrato_model
                                        ->obter_contrato_devedor_id($id);

            //Negociações
            $id_contratos = array_map(function($item)
            { return $item['id'];}, $result['contratos']);
            $negociacoes = $this->devedornegociacao_model
                                ->obter_negociacao_devedor_id($id_contratos);

            //Parcelas
            $id_parcelas = array_map(function($item)
            { return $item['id']; }, $negociacoes);
            $parcelas = $this->devedorparcelas_model
                             ->obter_parcelas_negociacao_id($id_parcelas);


            for($i = 0; $i < count($negociacoes); $i++)
            {
                $id = $negociacoes[$i]['id'];
                $negociacoes[$i]['parcelas'] = 
                    array_filter($parcelas, function($a) use ($id){
                        return $a['negociacao_id'] == $id;
                    });
            }
            for($i = 0; $i < count($result['contratos']); $i++)
            {
                $id = $result['contratos'][$i]['id'];
                $result['contratos'][$i]['negociacoes'] =
                    array_filter($negociacoes, function($a) use ($id){
                        return $a['contrato_id'] == $id;
                    });

            }           

            return $result;
        }

    }

in this summary method you notice that I have considered the 3 classes that are related as I have said and need to be united to the generation of the , the logic and use of the array_filter to build relationships and finally call on controller:

public function de()
{
    $this->load->model('Devedordados_model');

    echo json_encode($this->Devedordados_model->obter_todos_dados_por_id(1),
                              JSON_PRETTY_PRINT);

}
  • Virgilio, sorry I’m late, I was out. E.. Worked, perfect!! Thanks

  • A doubt, the jump of the cat to build the json is in the for. for($i = 0; $i < count($negociacoes); $i++) and for($i = 0; $i < count($result['contratos']); $i++) ?

  • @Wagnerchild yes, but array_filter too. From a.lida in the manual

  • Using the form you gave me, I have 2 trades registered, but when I get this, one negotiation comes with its parcels in array, but the other comes with its parcels as object. Is that correct? Can’t everything come as array ? See image: https://uploaddeimagens.com.br/images/001/087/921/full/array.png?1505321555

  • @Wagnerson in the example I gave you note that this result_array so the example this right should be something in your code

  • Can you help me with the question? https://answall.com/questions/252844/obtains-array-e-n%C3%A3o-like-object-codeigniter

Show 1 more comment

Browser other questions tagged

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