I am studying some way to insert some information into the database and retrieve the ID, so that this ID is inserted into another table.
I need to know if this is possible, so as not to waste time on something if it was not possible to do:
Considering the array below:
Array
(
[0] => stdClass Object
(
[id] => 0
[atraso_inicial] => 1
[atraso_final] => 30
[multa] => 2.00
[juros] => 1.00
[honorario] => 0.00
[plano] => Array
(
[0] => stdClass Object
(
[id] => 0
[parcela] => 1
[ajuste] => 0.00
[entrada_minima] => 0.00
[desconto_multa] => 0.00
[desconto_juros] => 0.00
[desconto_principal] => 0.00
[desconto_honorario] => 0.00
[fase_id] => 1
[crud] => C
)
)
[campanha_id] => 1
[crud] => C
)
[1] => stdClass Object
(
[id] => 0
[atraso_inicial] => 31
[atraso_final] => 60
[multa] => 2.00
[juros] => 1.00
[honorario] => 0.00
[plano] => Array
(
[0] => stdClass Object
(
[id] => 0
[parcela] => 1
[ajuste] => 0.00
[entrada_minima] => 0.00
[desconto_multa] => 0.00
[desconto_juros] => 0.00
[desconto_principal] => 0.00
[desconto_honorario] => 0.00
[fase_id] => 1
[crud] => C
)
)
[campanha_id] => 1
[crud] => C
)
)
I need to define in the [phase_id] item of the [plan] object, the value of the id of the key [0], and do the same thing with the key 1, and so on.
If I had the value of [id], before inserting, it would be easy, the problem is that at this point, I'm going to enter the data for the first time, so I do not have the ID yet.
What I have so far is the following controller and modal:
controller
if (!empty($campanha[0]))
{
$campanha_id = $this->empresa->add_campanha($campanha, $empresa_id);
foreach ($campanha as $camp => $objeto_campanha)
{
$fase = $objeto_campanha->fase;
$this->empresa->add_fase($fase, $campanha_id);
foreach ($fase as $fas => $objeto_fase)
{
$plano = $objeto_fase->plano;
$this->empresa->add_plano($plano);
}
}
}
model
// add_campanha
public function add_campanha($dados, $empresa_id)
{
$array = (array) $dados;
foreach ($dados as $camp)
{
$campanha = [
'nome' => $camp->nome_campanha,
'empresa_id' => $empresa_id,
];
$this->db->insert($this->tb_campanha, $campanha);
$campanha_id = $this->db->insert_id();
return $campanha_id;
}
}
// add_fase
public function add_fase($dados, $campanha_id)
{
print_r($dados);
$array = (array) $dados;
foreach ($dados as $fase)
{
$fase = [
'atraso_inicial' => $fase->atraso_inicial,
'atraso_final' => $fase->atraso_final,
'multa' => $fase->multa,
'juros' => $fase->juros,
'honorario' => $fase->honorario,
'campanha_id' => $campanha_id
];
$this->db->insert($this->tb_fase, $fase);
$fase_id = $this->db->insert_id();
return $fase_id;
}
}
// add_plano
public function add_plano($dados)
{
$array = (array) $dados;
foreach ($dados as $plan)
{
$plano = [
'parcela' => $plan->parcela,
'ajuste' => $plan->ajuste,
'entrada_minima' => $plan->entrada_minima,
'desconto_juros' => $plan->desconto_juros,
'desconto_multa' => $plan->desconto_multa,
'desconto_principal' => $plan->desconto_principal,
'desconto_honorario' => $plan->desconto_honorario,
'fase_id' => $plan->fase_id,
];
$this->db->insert($this->tb_plano, $plano);
}
}
table
/* campanha */
DROP TABLE IF EXISTS `tb_campanha`;
CREATE TABLE IF NOT EXISTS `tb_campanha` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`nome` VARCHAR(255) NOT NULL,
`empresa_id` INT(11) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARSET = utf8;
/* fase */
DROP TABLE IF EXISTS `tb_fase`;
CREATE TABLE IF NOT EXISTS `tb_fase` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`atraso_inicial` INT(10) UNSIGNED NOT NULL,
`atraso_final` INT(10) UNSIGNED NOT NULL,
`multa` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`juros` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`honorario` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`campanha_id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARSET = utf8;
/* plano */
DROP TABLE IF EXISTS `tb_plano`;
CREATE TABLE IF NOT EXISTS `tb_plano` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`parcela` INT(10) UNSIGNED NOT NULL,
`ajuste` DECIMAL(10,2) NOT NULL,
`entrada_minima` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`desconto_juros` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`desconto_multa` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`desconto_principal` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`desconto_honorario` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`fase_id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARSET = utf8;
