2

I have a service which am passing a model and an identifier (optional) to.

The model contains new data and the aim is that if an existing record exists then we merge the two responses into a json_encoded string for storage. There is some jiggery pokery going on within the service to check against existing records before merging the results and then saving to either 1) a new record (if no id) or 2) update the existing record.

All well so far until it comes to saving the record. This returns true within my code but existing records within the database are not saved/updated.

Any thoughts on why this might be happening appreciated? Service code here:

public function saveReport(MS_ReportModel $model,$id = null) {
  if ($id > 0) {
    $tmp = [];
    $record = MS_ReportRecord::model()->findByAttributes(array('id' => $id));
    $fields = json_decode($record->report);
    // merge jiggery pokerey here //
    $record->setAttribute('report',json_encode($merge));
  } else {
    $record = new MS_ReportRecord;
  }
  // add other attributes here //
  if ($record->save()) {
    return $record->id;
  } else {
    return false;
  }
}

I've found a similar thread at Craft CMS Plugin Save Record to DB from Variable but cannot reproduce the solution discussed.

Cannot see for the life of me why the record is saving (i.e. returning a true response) but the relevant values are not being reflected in the database. If I output the $record object at any stage after the merge has taken place it contains the values I expect.

Ta, Cole

9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.