Codeigniter enter registration in bank

Asked

Viewed 179 times

2

Hello, I work with Codeigniter 3.

I have a question.

We assume the model below:

class Peca_model extends CI_Model 
{
public $Id;
public $Data_registro;
public $Ativo;
public $Nome;

public function set_peca()
{
    if(empty($this->Id))
        return $this->db->insert('Peca', $this);
    else
    {
        $this->db->where('Id', $this->Id);
        return $this->db->update('Peca', $this);
    }
}
}

Well, that’s the way I insert a record into the database. That up there works, tested and is ok.

Now below follows the same model, with a small modification: a more attribute added to it.

class Peca_model extends CI_Model 
{
public $Id;
public $Data_registro;
public $Ativo;
public $Nome;
public $Transacoes;    

public function set_peca()
{
    if(empty($this->Id))
        return $this->db->insert('Peca', $this);
    else
    {
        $this->db->where('Id', $this->Id);
        return $this->db->update('Peca', $this);
    }
}
}

NOTE: MY TABLE PIECE IN THE DATABASE IT DOES NOT HAVE THIS ATTRIBUTE TRANSACTIONS.

Well, if I try to make an insert of some record in the database, the following error occurs:

Unknown column 'Transactions' in 'field list'

Of course, one can expect that mistake.

Now that my problem is explained I would like to know how to do (if it is possible in codeigniter) inform somehow that the attribute Transactions is not part of the record to be entered. I would like to use this attribute only as an attribute to be filled by an object returned by another model (the object returned by another model would be according to Peca_id).

The example is pretty silly, but that’s the idea.

Can anyone tell me if there is a way to resolve this? How to ignore a class attribute when doing Insert or update whatever.

From now on, thank you.

1 answer

1


I figured out how to do this. It’s very simple. I decided to share it with you, because maybe there are more people with the same doubt.

The class below as I mentioned earlier, when trying to make the Insert or update of some record gave an error, because as I said, $Transactions is not a field in my table in the database but is a related object. That is, for each piece I can have a list of transactions. Below is the class with the problem solved.

class Peca_model extends CI_Model 
{
    public $Id;
    public $Data_registro;
    public $Ativo;
    public $Nome;
    public $Transacoes;    

    public function __construct()
    {
        $this->load->model('Transacao_model');
        $this->Trasacoes= $this->Transacao_model;
    }
    public function set_peca()
    {
        if(empty($this->Id))
            return $this->db->insert('Peca', $this);
        else
        {
            $this->db->where('Id', $this->Id);
            return $this->db->update('Peca', $this);
        }
    }
}

Note that there is now a constructor responsible for loading an instance of the related object into the $Transactions attribute. By doing this, Insert/update will ignore the $transactions attribute.

In the above class the problem was solved, now the usefulness for this would be if we had a method that would return a part that would also contain its list of transactions. By doing this we leave everything in one object instead of making multiple model method calls in the controller to retrieve related records. That is, when it is an object that has some relationship with another, it already returns the two in a single call in the model.

Thank you all.

Browser other questions tagged

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