<p>There is a second parameter  in your <code>setAttributes()</code> function. This parameter is true per default and checks if the attribute is <code>safe</code> so you either have to declare all safe attributes in your records rules or you set the parameter to false. </p>

CC BY-SA 3.0
2018-03-14T21:39:50.757Z
Thank you Robin! That was precisely the issue! I'll have to learn more about what the 'safe' rules are but in the meantime false fixed it up.
Shift2Design
CC BY-SA 3.0
2018-03-14T21:47:50.260Z
The craft docs on setAttributes did not have the value but looking at yii docs I see it http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail
Shift2Design
CC BY-SA 3.0
2018-03-14T22:23:08.683Z
Don't get me wrong, I love Craft and I love to work with it but the online documentation is pretty bad. Most things are well explained in the code, so you should look there first but nearly 70% is not even mentioned in the online documentation. As for the safe attributes, you can learn more about that here http://www.yiiframework.com/doc-2.0/guide-structure-models.html#safe-attributes
Robin Schambach
CC BY-SA 3.0
2018-03-14T21:34:44.873Z
2018-03-14T21:34:44.873Z
Robin Schambach
<p>I am setting attributes on my record using a models' attributes (that has validated successfully). I cannot figure out why the values aren't assigned. I can manually assign each one but that's redundant.</p>

<pre><code>// Set attributes
// Massive assignment will only be made for fields which have passed some explicit validation rule.
if ($model-&gt;validate())
{
    $purchaseRecord-&gt;setAttributes($model-&gt;getAttributes());
    var_dump($model-&gt;snipInvoiceNumber); // works
    var_dump($purchaseRecord-&gt;snipInvoiceNumber); // null value
    $purchaseRecord-&gt;snipInvoiceNumber = $model-&gt;snipInvoiceNumber; // works
} 
</code></pre>

<p>UPDATE:
Per comments on this thread I looped through and set the attributes individually. Anyone have insight on why setAttributes didn't work? 
<a href="https://craftcms.stackexchange.com/questions/8884/save-contact-form-data-to-database">Save contact form data to database</a></p>

<p>This is what I used instead of setAttributes (plural).</p>

<pre><code>foreach ($model-&gt;getAttributes() as $key =&gt; $value ) {
    $purchaseRecord-&gt;setAttribute($key, $value);
}
</code></pre>

CC BY-SA 3.0
2018-03-14T18:06:15.917Z
It would seem while several of these columns will not be assigned values successfully a handful of others are working fine. Not sure if the issue is related but the ones working are all ones defined in my record defineRelations().
Shift2Design
<models><attribute>
Model values assignment issue
# Model values assignment issue

(Asked by: username_0 on 2018-03-14)

I am setting attributes on my record using a models' attributes (that has validated successfully). I cannot figure out why the values aren't assigned. I can manually assign each one but that's redundant.
// Set attributes
// Massive assignment will only be made for fields which have passed some explicit validation rule.
if ($model->validate())
{
    $purchaseRecord->setAttributes($model->getAttributes());
    var_dump($model->snipInvoiceNumber); // works
    var_dump($purchaseRecord->snipInvoiceNumber); // null value
    $purchaseRecord->snipInvoiceNumber = $model->snipInvoiceNumber; // works
} 

UPDATE:
Per comments on this thread I looped through and set the attributes individually. Anyone have insight on why setAttributes didn't work? 
Save contact form data to database
This is what I used instead of setAttributes (plural).
foreach ($model->getAttributes() as $key => $value ) {
    $purchaseRecord->setAttribute($key, $value);
}

**Tags:** models, attribute

> **Comments:**
> **username_0**: It would seem while several of these columns will not be assigned values successfully a handful of others are working fine. Not sure if the issue is related but the ones working are all ones defined in my record defineRelations().  

---

## 1 answers

### Answer by username_1 on 2018-03-14. Score: 3 | Accepted answer

There is a second parameter in your `setAttributes()` function. This parameter is true per default and checks if the attribute is `safe` so you either have to declare all safe attributes in your records rules or you set the parameter to false.

> **Comments:**
> **username_0**: Thank you Robin! That was precisely the issue! I'll have to learn more about what the 'safe' rules are but in the meantime false fixed it up.  
> **username_0**: The craft docs on setAttributes did not have the value but looking at yii docs I see it http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail  
> **username_1**: Don't get me wrong, I love Craft and I love to work with it but the online documentation is pretty bad. Most things are well explained in the code, so you should look there first but nearly 70% is not even mentioned in the online documentation. As for the safe attributes, you can learn more about that here http://www.yiiframework.com/doc-2.0/guide-structure-models.html#safe-attributes  

---

