CodeIgniter - Message: Trying to get property of non-object

0

I'm trying to solve this error:

  

Severity: Notice   Message: Trying to get property of non-object   Filename: contracts / visual.php   Line Number: 37

CODE:

contract_model.php

public function getById($id){

    $this->db->where('idContrato',$id);
    $this->db->limit(1);
    $consulta = $this->db->get('contrato')->result();

            foreach($consulta as &$valor){
                    $this->db->where('idCliente', $valor->idCliente);
                    $valor->curso = $this->db->get('cliente')->result();
            }

            return $consulta;

}

contracts.php

public function visualizar(){

    if(!$this->permission->checkPermission($this->session->userdata('permissao'),'vContrato')){
       $this->session->set_flashdata('error','Você não tem permissão para visualizar Contratos.');
       redirect(base_url());
    }

    $this->data['custom_error'] = '';
    $this->data['result'] = $this->contratos_model->getById($this->uri->segment(3));
    $this->data['view'] = 'contratos/visualizar';
    $this->load->view('tema/topo', $this->data);

}

visual.php

<?
    print_r($result);
?>

Viewer print_r return

  

Array ([0] => stdClass Object ([idContract] => 3 [idClient] => 3 [idChange] => 2 [payout_form] => 96 [fiscal_note] => 94 [ > 1 [tabs] => 0 [status] => 0 [course] => Array ([0] => stdClass Object 3 [Raisosocial] => PIXX CREATIVE SOLUCOES LTDA [nomefantasia] => PIXX CREATIVE SOLUCOES LTDA [cnpj] => 0655544801120 [ie] => 0 [im] => 0 [ > [address_number] => [address_number] => [[address_completion]]>> [ [address_number] => 0 [address_number] => [address_number] => > = [Gmail] => gmail => gmail => gmail => gmail => gmail => gmail => gmail = => 4 [fat_endereco] => [fat_endereco_number] => 0 [fat_endereco_complemento] => [fat_endereco_boirro] => [fat_endeco_number] => ereco_cep] = > 0 [fat_endereco_city] = > [fat_endereco_estado] = > 12 [fat_responsible] = > [fat_telefone_ddd] = > 0 [fat_telephone] = > 0 [fat_cellular_ddd] = > 0 [fat_cellular] = > 0 [email_finance] = > [maturity] = > 1 [note] = > 94 [closing_of] = > 1 [closure_a] = > 1 [guides] = > 0 [normal_moval_value] = > 0 [metro_moto_value] = > 0 [value_moto_depois_18] = > 0 [value_moto_km] = > 0 [normal_car_value] = > 0 [metropolitan_car_value] = > 0 [car_value_18] = > 0 [car_value] = > 0 [normal_value_value] = > 0 [Metric value] = > 0 [trail_value_18] = > 0 [value_caminhao_km] = > 0 [status] = > 0))))

I also pasped PasteBin

I've been trying for days and I can not print the data correctly.

Given a echo $result->campo
Here's the message above.

    
asked by anonymous 08.06.2015 / 21:48

5 answers

0

Your object is inside an array item, in which case you can call it directly by the key.

It seems to me that the definition is made here:

$this->data['result'] = this->contratos_model->getById($this->uri->segment(3));

In your view it looks like this:

echo $result[0]->idContrato;
    
08.06.2015 / 22:14
0

Try this:

print_r ($ result [0] -> field);

There is another object nested in the same way in the client array, so you can access it as follows:

$ result [0] -> client [0] -> field

    
08.06.2015 / 22:10
0

The final version of getById looks like this    public function getById ($ id) {

    $this->db->where('idContrato',$id);
    $this->db->limit(1);
    $consulta = $this->db->get('contrato')->result();


    foreach($consulta as &$valor){
        $this->db->where('idCliente', $valor->idCliente);
        $valor->cliente = $this->db->get('cliente')->result();

        $sql = "SELECT 
                    cf.*, 
                    f.idFuncionario, 
                    f.nome as nomeFuncionario 
                FROM 
                    contrato_funcionario as cf, 
                    funcionario as f 
                WHERE 
                    cf.idContrato = '".$valor->idContrato."'
                AND
                    cf.idFuncionario = f.idFuncionario 
                GROUP BY 
                    f.idFuncionario";
        $valor->funcionarioLista = $this->db->query($sql)->result();
    }


    return $consulta;

}

And it worked.

    
08.06.2015 / 22:51
0

You can also use the query:

$valor->cliente = $this->db->get('cliente')->row();

Then it takes the value directly from one line only. The ->result(); invariably generates an array indexed by numerical values: [0], [1], etc.

    
16.04.2017 / 14:46
0

You are returning via array so in this case you need to use the loop in array for example using the while instead of foreach, or return string to loop with foreach. ex: return $ query-> result ();

    
04.02.2018 / 14:41