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 perform poorly and does not access the database too much, with 3 sql and PHP functions ( array_filter ), you can have the expected result, I will propose a minimum example should be reflected in your question by the first response :
Database Diagram
Thesetableshave1-to-manyrelationshipsandneedtobelinkedtogethertogeneratea json with all the information of a certain devedor
( $devedor_id
).
Classes
Contract Debtor
<?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();
}
}
Debtor Negotiations
<?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 Parcels
<?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 summarized method you notice that I looked at the 3 classes that are related as I had said and need to be joined for the generation of json , we used the logic and use of the array_filter
function to construct the relations 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);
}