CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

From_form update affects all records

Open Wouter60 opened this issue 6 years ago • 4 comments

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?

Wouter60 avatar Sep 02 '19 21:09 Wouter60

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

avenirer avatar Sep 03 '19 09:09 avenirer

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.

Wouter60 avatar Sep 03 '19 13:09 Wouter60

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

venonw avatar Oct 26 '19 11:10 venonw

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.

Wouter60 avatar Aug 15 '23 20:08 Wouter60