Error Call to a Member Function Row() on a non-object Codeigniter

Asked

Viewed 685 times

0

Password change screen, form is ok, but variable to fetch information in bd giving error, in function Row()!

Follow VIEW:

case 'alterar_senha':
        $iduser= $this->uri->segment(3);
        if($iduser=NULL):
          set_msg('msgerro', 'Escolha um usuário para alterar', 'erro');
          redirect('usuarios/gerenciar');
        endif; ?>
        <div class="large-12 columns">
            <?php if (is_admin() || $iduser == $this->session->userdata('user_id')):
                $query = $this->usuarios->get_byid($iduser)->row();
                erros_validacao();
                get_msg('msgok');
                echo form_open(current_url(), array('class'=>'custom'));
                echo form_fieldset('Alterar Senha');
                echo '<div class="large-7 columns">';
                echo form_label('Nome completo');
                echo form_input(array('name'=>'nome', 'disabled'=>'disabled'), set_value('nome', $query->nome));
                echo form_label('Email');
                echo form_input(array('name'=>'email', 'disabled'=>'disabled'), set_value('email', $query->email));
                echo '<div class="large-7 columns>"';
                echo form_label('Login');
                echo form_input(array('name'=>'login',  'disabled'=>'disabled'), set_value('login', $query->login));
                echo form_label('Nova Senha');
                echo form_password(array('name'=>'senha'), set_value('senha'), 'autofocus');
                echo form_label('Repita a senha');
                echo form_password(array('name'=>'senha2'), set_value('senha2'));
                echo anchor('usuarios/gerenciar', 'Cancelar', array('class'=> 'button radius alert espaco'));
                echo form_submit(array('name'=>'alterarsenha', 'class'=>'button radius '), 'Salvar dados');
                echo form_hidden('idusuario', $iduser);
                echo form_fieldset_close();
                echo form_close();
            else:
                redirect('usuarios/gerenciar');
            endif;  ?>
        </div>

        <?php
        break;

Follows CONTROLLER with function:

public function alterar_senha(){
    esta_logado();
    set_tema('titulo','Alteração de senha');
    set_tema('conteudo',load_modulo('usuarios','alterar_senha'));
    load_template();
}

FOLLOWS MODEL :

public function get_byid($id=NULL) {
        if ($id != NULL):
            $this->db->where('id', $id);
            $this->db->limit(1);
            return $this->db->get('usuarios');
         else:
            return FALSE;   
         endif;
    }

2 answers

0

Wallace, man, I found the mistake, it’s part here of the first if , is missing a sign of "="... if($iduser=NULL):, put and worked normal! Thanks for the help!

0


This happens because the function get_byid does not return a objeto at some point, and yes a FALSE. Probably this generates an error.

Instead, if no result is returned you should launch a Exception within the get_byid.

I realize that there is a structure error in your code. You should define the data you will use in the controller view, and not query in the view.

So, keeping the current code in the `Controller, you should do the following check:

$query = $this->usuarios->get_byid($iduser);

if ($query !== FALSE) {
    $usuario = $query->row();
}
  • Keeping the same code and checking , returned the same error.

  • I tried to change the structure by setting the data in the controller, just calling the function in the View but generates the same error too

  • Changed the view code too, taking only the variable $usuario, instead of the section where you call row?

  • It had changed but with a wrong variable, now I put and no longer gave the error. Only that did not return to me the fields filled, login user and email

Browser other questions tagged

You are not signed in. Login or sign up in order to post.