Answers.Body: <p>It is definitely <em>a</em> way to save record. :)</p>
  
  <p>You might want to be more explicit with validation, though.  i.e.</p>
  
  <pre><code>if ($record-&gt;validate())
  {
      // Save record
      if ($record-&gt;save(false))
      {
          // Populate id
          if($isNewGoal)
          {
              $model-&gt;id = $record-&gt;id;
          }
  
          return true;
      }
      else
      {
          return false;
      }
  }
  else
  {
      $model-&gt;addErrors($record-&gt;getErrors());
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T16:52:37.920Z
Answers.Text: Makes sense, I'm validating the model before calling saveGoal. Need to validate both?
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T16:53:42.547Z
Answers.Text: If the attributes for each are identical, then probably not.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T16:57:33.490Z
Answers.Text: They are. Still trying to get my head around models and records and all the different ways of working with them. So the model passed from the controller is available in both controller and services?
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T18:50:59.927Z
Answers.Text: Right - think of a model as a the thing that you can freely pass between the different layers of your application (controllers, services, etc.).
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T16:42:24.833Z
Answers.LastActivityDate: 2015-04-07T16:42:24.833Z
Answers.UserDisplayName: Brad Bell
Body: <pre><code>public function saveGoal(Logic_GoalModel &amp;$model)
  {
      // New goal
      $isNewGoal = !$model-&gt;id;
  
      // Populate record
      $record = $this-&gt;getGoalRecordById($model-&gt;id);
      $record-&gt;userId = $model-&gt;userId;
      $record-&gt;goal = $model-&gt;goal;
      $record-&gt;complete = $model-&gt;complete;           
  
      // Save record
      if($record-&gt;save(false))
      {
          // Populate id
          if($isNewGoal)
          {
              $model-&gt;id = $record-&gt;id;
          }
  
          return true;
      }
      else
      {
          return false;
      }
  }
  
  private function getGoalRecordById($id = null)
  {
      if($id)
      {
          $record = Logic_GoalRecord::model()-&gt;findById($id);
  
          if (!$record)
          {
              throw new Exception(Craft::t('No record exists with the ID “{id}”', array('id' =&gt; $id)));
          }
      }
      else
      {
          $record = new Logic_GoalRecord();
      }
  
      return $record;
  }
  </code></pre>
  
Tags: <plugin-development>
Title: Is this the correct and safest way to update / save a new record?
ThreadText: # Is this the correct and safest way to update / save a new record?
  
  (Asked by: username_0 on 2015-04-07)
  
  ```
  public function saveGoal(Logic_GoalModel &$model)
  {
      // New goal
      $isNewGoal = !$model->id;
  
      // Populate record
      $record = $this->getGoalRecordById($model->id);
      $record->userId = $model->userId;
      $record->goal = $model->goal;
      $record->complete = $model->complete;           
  
      // Save record
      if($record->save(false))
      {
          // Populate id
          if($isNewGoal)
          {
              $model->id = $record->id;
          }
  
          return true;
      }
      else
      {
          return false;
      }
  }
  
  private function getGoalRecordById($id = null)
  {
      if($id)
      {
          $record = Logic_GoalRecord::model()->findById($id);
  
          if (!$record)
          {
              throw new Exception(Craft::t('No record exists with the ID “{id}”', array('id' => $id)));
          }
      }
      else
      {
          $record = new Logic_GoalRecord();
      }
  
      return $record;
  }
  
  ```
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-07. Score: 1 | Accepted answer
  
  It is definitely *a* way to save record. :)
  
  You might want to be more explicit with validation, though. i.e.
  
  ```
  if ($record->validate())
  {
      // Save record
      if ($record->save(false))
      {
          // Populate id
          if($isNewGoal)
          {
              $model->id = $record->id;
          }
  
          return true;
      }
      else
      {
          return false;
      }
  }
  else
  {
      $model->addErrors($record->getErrors());
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Makes sense, I'm validating the model before calling saveGoal. Need to validate both?  
  > **username_1**: If the attributes for each are identical, then probably not.  
  > **username_0**: They are. Still trying to get my head around models and records and all the different ways of working with them. So the model passed from the controller is available in both controller and services?  
  > **username_1**: Right - think of a model as a the thing that you can freely pass between the different layers of your application (controllers, services, etc.).  
  
  ---
  
  