Variable with null value when calling model method

Asked

Viewed 19 times

0

I’m working with codeigniter. By submitting a form I am sending the data to model through getters and setters.

This is my input

div class="col-md-6">
                <div class="form-group">
                    <?php echo form_label('Fornecedores (CNPJ - Razão Social):*', 'fornecedor'); ?>
                    <?php echo form_dropdown('fornecedor', $fornecedores, set_value('fornecedor'), array("class" => 'form-control', 'required' => 'required'))?>
                </div>
            </div>

When clicking on Ubmit, enter a method in the controller has this code:

 $this->load->model('Movimento_model', 'movimento');
 $this->movimento->setCnpj($this->input->post('fornecedor'));

 echo $this->movimento->getCnpj(); // exibe para teste

This is my model (summarized):

class Movimento_model extends CI_Model {

 private $cnpj;

 public function setCnpj($cnpj)
{
    $this->cnpj = strtoupper(trim($cnpj));
}

 public function getCnpj()
{
    return $this->$cnpj; // LINHA DO ERRO
}

This is the mistake:

Undefined variable: cnpj

Note: I am setting but it seems that you are not accepting. When I try to show that the error occurs.

1 answer

-1

Guy tries to arrow an initial value to the variable

class Movimento_model extends CI_Model {

private $cnpj;
$this->cnpj = 0;

public function setCnpj($cnpj)
{
    $this->cnpj = strtoupper(trim($cnpj));
}

public function getCnpj()
{
    return $this->$cnpj; // LINHA DO ERRO
}

Browser other questions tagged

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