Welcome Guest, Not a member yet? Register   Sign In
Model and Active Record with private properties.

Hi there,

I am developing a model to insert a record in the database. I am using private property and see that the "insert" the object "db" generates the error "You must use the" set "method to update an entry". My question is: I can not use private property and protected in my model? Only public?


Rafael Wendel Pinheiro

I don't understand what does set() method have to do with private and protected properties. Could you show us the model?

Since you mentioned Active Record, I'm assuming you're using CI 2 instead of CI 3.

In your model, $this->db refers to CI's database library, so, if you wanted to insert something using CI's Active Record methods, you would use
$this->db->insert('table_name', $data);


$data can be an object or an array, but, if it's an object, the object must have a set of public properties which match the column names in your database (if it's an array, the keys in the array would match the column names in the database).

The message you are alluding to, though, implies you are performing an update via
$this->db->update('table_name', $data);


Whether performing an insert or an update, you can either supply the $data to the insert()/update() method, or use the set() method to set individual columns:
$this->db->set('column_name', $value);

(The set() method is documented on the same page as the insert() and update() methods, just scroll up from update() or scroll down from insert().)

In any case, all properties and methods defined within your model are available to any other method in your model. The access modifiers (public/protected/private) only define whether other classes can access those properties/methods.



First, thank you for your help. I'm using CI 3 and had this doubt. But now I think I did.

Thanks again.

Rafael Wendel Pinheiro

CI 3 would change the links, and it's called Query Builder instead of Active Record, but the basic use of $this->db is the same.

Theme © iAndrew 2016 - Forum software by © MyBB