avenirer / CodeIgniter-MY_Model

Base model (MY_Model) for the Codeigniter framework.
325 stars 205 forks source link

From_form update affects all records #291

Open Wouter60 opened 5 years ago

Wouter60 commented 5 years ago

I've setup a model based on MY_Model to insert and update records from form. Inserting a new record works fine, but when I want to update a record, all field values from the form are written to all records in the table. I went throught the model code, the controller and the view several times, but I can't find where I'm doing something wrong. Can you help me fix this?

avenirer commented 5 years ago

Did you pass the id of the row? The primary key...

Wouter60 commented 5 years ago

Yes, I did. In the controller I have:

$updated = $this->gemeenten_model->from_form(NULL,NULL,array('id'=>$id))->update();

$id is passed to the controller function as the third segment. I've checked if $id has a value. That's always the case.,

In my 'gemeenten_model':

public $table = 'gemeenten; public $primary_key = 'id'; public $fillable = array('plaats','gemeente_naam');

I checked the query that is generated by the model, with $this->db->last_query(). The outcome of that is: UPDATE gemeenten SET gemeente_naam = 'Testgemeente', plaats = 'Testplaats', updated_at = '2019-09-03 15:34:27'

So no sign of the primary key here.

venonw commented 4 years ago

change below line: $updated = $this->gemeenten_model->from_form(NULL,array('id'=>$id),array('id'=>$id))->update();

Wouter60 commented 1 year ago

change below line: $updated = $this->gemeenten_model->from_form(NULL,array('id'=>$id),array('id'=>$id))->update();

No, this doesn't work either. Creating a record with the from_form method works flawlessly, but if I update one record, all records in the table are affected, i.e. they become identical to the one that I wanted to update.