<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>

CC BY-SA 3.0
2015-04-07T16:52:37.920Z
Makes sense, I'm validating the model before calling saveGoal. Need to validate both?
Sam Hibberd
CC BY-SA 3.0
2015-04-07T16:53:42.547Z
If the attributes for each are identical, then probably not.
Brad Bell
CC BY-SA 3.0
2015-04-07T16:57:33.490Z
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?
Sam Hibberd
CC BY-SA 3.0
2015-04-07T18:50:59.927Z
Right - think of a model as a the thing that you can freely pass between the different layers of your application (controllers, services, etc.).
Brad Bell
CC BY-SA 3.0
2015-04-07T16:42:24.833Z
2015-04-07T16:42:24.833Z
Brad Bell
<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>

<plugin-development>
Is this the correct and safest way to update / save a new record?
# 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.).  

---

