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 sql 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
these tables have 1-to-many relationships and need to be joined together to generate a json 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 json, 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);
}
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 ?– Leandro Lima
@Leandrolima, eh this, but recover the way it is in the other question.
– Wagner Fillio
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 ?
– Leandro Lima
@Leandrolima, updated Link. I know how to make relationships of tables, but I believe this is not the case.
– Wagner Fillio
see this image, to give you an idea. https://uploaddeimagens.com.br/imagens/img-png-129
– Wagner Fillio
logs
what will be the parameter for such recovery? and 3) which fields of the tablelogs
will be related and recovered to display that fieldview
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– novic
– Wagner Fillio
I placed the table of
email
as an example, but thisview
, will be applied in the tablecontrato
, and thisview
will display the name of the company, whose fieldID
is present in the contract table.empresa_id
. Take this JSON example and you will see an example: https://pastebin.com/embed_js/VMf30K5– Wagner Fillio
@Virgilionovic, can you help me create this
view
? creating this, I can already manage with the rest.– Wagner Fillio
Wagner yes but do the following put the two tables involved + relation + fields obtained there I promote an ok answer
– novic
@Virgilionovic did an update on the question, can see if it helps ?
– Wagner Fillio
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?
– novic
@Virgilionovic, exactly. As in negotiations, I need to integrate the plots.. i need to get the contract negotiations and respectively the splits of negotiations
– Wagner Fillio
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
– Wagner Fillio
Did it work ?? Wagner?
– novic