1

I have a list of financial launch. Each launch may or may not have multiple payments. So for every financial launch I look for a list of payments. I would like this list to be within its corresponding financial launch.

My complete function:

    public function customerInvoice()
    {
        $_customer = filtra_int($this->input->post('cliente_id'));
        $this->redireciona_id_nula($_customer, $this->url . '/ficha');
        $_view = [];
        $this->load->helper(['filter', 'string', 'currency', 'validate', 'phone']);

        $_view['glyph'] = 'user';
        $_view['enterprise'] = $this->enterprise;
        $_view['buttons'] = $this->_getFormButtons($_customer, $this->url . '/ficha');

        $this->load->model('lancamento_model', 'lancamentoModel');
        $_view['releases'] = $this->lancamentoModel->getCustomerRelease($_customer);
        foreach ($_view['releases'] as $i => $value) {
//            $_view['releases']['order'][$i] = $this->lancamentoModel->getCustomerOrder($value->id);
             $value['order'] = $this->lancamentoModel->getCustomerOrder($value->id);
        }

        $this->addBreadcrumb('Lançamentos', base_url() . 'lancamentos');
        $this->addBreadcrumb('Ficha', base_url() . 'lancamentos/ficha');
        $this->addBreadcrumb('Exibir');

        $this->extras['css'][] = $this->load->view('lancamentos/consulta/ficha_cliente/form/css', null, true);
        $this->extras['scripts'][] = $this->load->view('lancamentos/consulta/ficha_cliente/form/js', $_view, true);

//        $pagina = $this->getHtmlPagina('Ficha cliente', $_view);
//        $this->load->view('lancamentos/consulta/ficha_cliente/view/view', $pagina);


        $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($_view));
    }

json result:

 {  
   "release":{  
      "0":{  
         "id":"380",
         "data_vcto":"2016-01-15",
         "data_emissao":"2016-01-15",
         "documento":"292\/1",
         "vlr":"67.00",
         "vlr_divida":"0.00"
      },
      "order":[  
         [  
            {  
               "id":"142206",
               "data_vcto":"2016-01-15 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"67.00",
               "tipo_pagamento_nome":"Dinheiro"
            },
            {  
               "id":"213",
               "data_vcto":"2016-01-13 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"22.00",
               "tipo_pagamento_nome":"Dinheiro"
            }
         ]
      ]
   }
}

I would like something like that

{  
   "release":{  
      "0":{  
         "id":"380",
         "data_vcto":"2016-01-15",
         "data_emissao":"2016-01-15",
         "documento":"292\/1",
         "vlr":"67.00",
         "vlr_divida":"0.00",
         "order":[  
            {  
               "id":"142206",
               "data_vcto":"2016-01-15 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"67.00",
               "tipo_pagamento_nome":"Dinheiro"
            },
            {  
               "id":"213",
               "data_vcto":"2016-01-13 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"22.00",
               "tipo_pagamento_nome":"Dinheiro"
            }
         ]
      }      
   }
}

it is possible ? And what do I have to do in the foreach?

Update: I did as @Yulio Aleman Jimenez suggested...but after that the error appeared

Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\beauty\application\controllers\Lancamentos.php on line 829

Message: Cannot use object of type stdClass as array

Error Print

Edi
  • 615
  • 5
  • 15

1 Answers1

0

I think you must change your code like this:

$_releases = $this->lancamentoModel->getCustomerRelease($_customer);
foreach ($_releases as $i => $value) {
    // try with this changes
    $value = $this->lancamentoModel->getCustomerOrder($value->id);
    $_releases[$i] = (object)array_merge((array)$_releases[$i], array('order' => $value));
}

The thing is to store the array of orders in the order of each release.

Update

You have working with objects, it's the reazon you have to cast to array, add new values in order and then cast to object.

Yulio Aleman Jimenez
  • 1,642
  • 3
  • 17
  • 33
  • i am getting this error: Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\beauty\application\controllers\Lancamentos.php:829). Line 829 is your code.. – Edi Nov 29 '18 at 19:52
  • Can you show the portion of code around the line 829? It seems that error doesn't related to the code of your initial question. – Yulio Aleman Jimenez Nov 29 '18 at 20:52
  • Check this answers to find a possible solution https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php, update your question to get more help or make another new question. – Yulio Aleman Jimenez Nov 29 '18 at 20:54
  • I update my question... had no mistakes before. I just wish I had my json in the format I said. After doing as you suggested the error started appearing. Any suggestion ? – Edi Nov 30 '18 at 22:56
  • I solved my problem: foreach ($_view['releases'] as $i => $value) { $_view['releases'][$i]->payments = $this->lancamentoModel->getCustomerOrder($value->id); }. Thanks for support! – Edi Dec 04 '18 at 00:37