Body: <p>I have a service which am passing a model and an identifier (optional) to.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Any thoughts on why this might be happening appreciated? Service code here:</p>
  
  <pre><code>public function saveReport(MS_ReportModel $model,$id = null) {
    if ($id &gt; 0) {
      $tmp = [];
      $record = MS_ReportRecord::model()-&gt;findByAttributes(array('id' =&gt; $id));
      $fields = json_decode($record-&gt;report);
      // merge jiggery pokerey here //
      $record-&gt;setAttribute('report',json_encode($merge));
    } else {
      $record = new MS_ReportRecord;
    }
    // add other attributes here //
    if ($record-&gt;save()) {
      return $record-&gt;id;
    } else {
      return false;
    }
  }
  </code></pre>
  
  <p>I've found a similar thread at <a href="https://craftcms.stackexchange.com/questions/10984/craft-cms-plugin-save-record-to-db-from-variable">Craft CMS Plugin Save Record to DB from Variable</a> but cannot reproduce the solution discussed.</p>
  
  <p>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 <code>$record</code> object at any stage after the merge has taken place it contains the values I expect.</p>
  
  <p>Ta, Cole</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T11:43:45.267Z
Comments.Text: PS the model/record passes validation. Have simply omitted this from the code excerpt
Comments.UserDisplayName: Cole Henley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T12:04:41.140Z
Comments.Text: If you have already validated it you can pass `false` into the save method to skip the internal validation stage: `$record->save(false)`
Comments.UserDisplayName: Josh Angell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T12:06:13.080Z
Comments.Text: Also, you can use `MS_ReportRecord::model()->findById($id);` instead ;)
Comments.UserDisplayName: Josh Angell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T14:39:15.630Z
Comments.Text: posted up code samples for the record and service at https://gist.github.com/cole007/bfd550edf6a0dd540afeb48442fc2165
Comments.UserDisplayName: Cole Henley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T15:49:04.333Z
Comments.Text: Found link to this problem being quite widespread in Yii http://stackoverflow.com/questions/8722354/yii-model-is-validating-but-data-could-not-be-saved
Comments.UserDisplayName: Cole Henley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T16:06:45.623Z
Comments.Text: In your code you linked to you are using `json_encode()` and storing json in a text field, instead try setting that column to `AttributeType::Mixed` and setting the naked array on it - Craft will then serialise it for you - that might be the issue but I’m just guessing now ...
Comments.UserDisplayName: Josh Angell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T16:16:25.940Z
Comments.Text: thanks @JoshAngell - tried that with no success. same problem happens even if trying to update another property (eg a numeric field/column)
Comments.UserDisplayName: Cole Henley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T17:51:04.453Z
Comments.Text: Just so I'm following, you're getting `MysteryShopperPlugin::Log('Report saved via service');` logged, but no updated/inserted data in the database? How is `$model` instantiated before it's passed into the service? Do you have any of this code executing in the context of a database transaction?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-17T09:32:52.670Z
Comments.Text: Hey @BradBell - `$model` is initiated through a controller that is looping through known instances/records within the database. if it finds a match then it passes new values to the service that get merged with any existing values which are then saved.
Comments.UserDisplayName: Cole Henley
Tags: <plugin-development><models><records>
Title: Custom plugin - cannot save record
ThreadText: # Custom plugin - cannot save record
  
  (Asked by: username_0 on 2017-03-16)
  
  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](https://craftcms.stackexchange.com/questions/10984/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, username_0
  
  **Tags:** plugin-development, models, records
  
  > **Comments:**
  > **username_0**: PS the model/record passes validation. Have simply omitted this from the code excerpt  
  > **username_1**: If you have already validated it you can pass `false` into the save method to skip the internal validation stage: `$record->save(false)`  
  > **username_1**: Also, you can use `MS_ReportRecord::model()->findById($id);` instead ;)  
  > **username_0**: posted up code samples for the record and service at https://gist.github.com/cole007/bfd550edf6a0dd540afeb48442fc2165  
  > **username_0**: Found link to this problem being quite widespread in Yii http://stackoverflow.com/questions/8722354/yii-model-is-validating-but-data-could-not-be-saved  
  > **username_1**: In your code you linked to you are using `json_encode()` and storing json in a text field, instead try setting that column to `AttributeType::Mixed` and setting the naked array on it - Craft will then serialise it for you - that might be the issue but I’m just guessing now ...  
  > **username_0**: thanks @username_1 - tried that with no success. same problem happens even if trying to update another property (eg a numeric field/column)  
  > **username_2**: Just so I'm following, you're getting `MysteryShopperPlugin::Log('Report saved via service');` logged, but no updated/inserted data in the database? How is `$model` instantiated before it's passed into the service? Do you have any of this code executing in the context of a database transaction?  
  > **username_0**: Hey @username_2 - `$model` is initiated through a controller that is looping through known instances/records within the database. if it finds a match then it passes new values to the service that get merged with any existing values which are then saved.  
  
  ---
  
  ## 0 answers
  
  