<p>I am just iterating on the previous answer to attempt to explain why this is happening. If you take a look at <code>BaseElementModel.php</code> at the <code>setContent()</code> method. You'll see that the core code is doing pretty much the exact thing as:</p>

<pre><code>$entry-&gt;getContent()-&gt;setAttributes(array(
    'foo' =&gt; 'bar',
    'xyz' =&gt; '123'
));
</code></pre>

<p>The core confusion seems to be coming from a misunderstanding of how Yii models work. When a model as attributes defined, only those attributes can be set. Any other attribute will be ignored. I am not sure if you are trying to set attributes on a model that doesn't exist, but I used the following and it seems to work for me.</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId = 4;
$entry-&gt;typeId = 4;

// Works because title exists in the content model
$entry-&gt;setContent([
    'title' =&gt; 'bar'
]);

// foo and xyz do not exist in the content model, so attributes are ignored
$entry-&gt;setContent([
    'foo' =&gt; 'bar',
    'xyz' =&gt; '123'
]);

var_dump($entry-&gt;getContent()-&gt;getAttributes());exit();
</code></pre>

<p>The reason why the following works is because you using PHP to set a public property on an object instance. So if you are finding yourself having to rely on something like this, check to see if the attributes exist in the content model using <code>var_dump</code> or something similar.</p>

<pre><code>$entry-&gt;getContent()-&gt;foo = 'bar';
</code></pre>

<p>You can see how the core method works here:</p>

<p><a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail">http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail</a></p>

CC BY-SA 3.0
2015-01-26T07:29:54.470Z
thanks for the in-depth explanation. Obviously Brandons answer is also correct, not sure if I can accept two answers as correct?
Sidney Widmer
CC BY-SA 3.0
2015-01-26T22:50:40.300Z
I upvoted Brandon's answer :). I would probably go for his answer being correct since he's the creator of Craft and knows the platform a lot better than I do.
Justin Kimbrell
CC BY-SA 3.0
2015-01-24T20:36:49.500Z
2015-01-24T20:36:49.500Z
Justin Kimbrell
<p>Have you tried using that form of setting with this method instead?</p>

<p><code>$entry-&gt;setContentFromPost(['foo' =&gt; 'bar', 'xyz' =&gt; '123']);</code></p>

<p>There's also this method:</p>

<pre><code>$entry-&gt;getContent()-&gt;setAttributes(array(
'foo' =&gt; 'bar',
'xyz' =&gt; '123'
));
</code></pre>

<p>As to the specific question of why doesn't setContent() work as expected, I can only guess that it may have to do with the values not being prepared properly.</p>

CC BY-SA 3.0
2015-01-23T15:07:38.833Z
2015-01-23T15:07:38.833Z
Michael Thomas
<p>You should be using <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer">setContentFromPost()</a> rather than <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail" rel="nofollow noreferrer">setContent()</a>, so that field types have a chance to prep the values that you are passing in.</p>

<p>See “<a href="https://craftcms.stackexchange.com/q/4486/9">What is getContent() / the ContentModel, and how do I use it to get and set custom field values?</a>” for more info on how this stuff works.</p>

CC BY-SA 3.0
2015-01-25T13:36:22.537Z
2015-01-25T13:36:22.537Z
2017-04-13T12:51:00.200Z
Brandon Kelly
<p>I'm a bit confused about the usage of these two. Let's say I want to create a new entry from a controller. For me, the nicer option would be to user something like this:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId = 4;
$entry-&gt;typeId = 4;

$entry-&gt;setContent([
    'foo' =&gt; 'bar',
    'xyz' =&gt; '456', 
]);

craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>Somehow this is not working for me. Instead, I have to use the uglier version like this:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId = 4;
$entry-&gt;typeId = 4;

$entry-&gt;getContent()-&gt;foo = 'bar';
$entry-&gt;getContent()-&gt;xyz = '123';

craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>Any ideas why the first example might be failing? Especially when working with more than two attributes, the first example would be way more easy to handle. </p>

<entries><services>
setContent() vs $entry->getContent()->foo
# setContent() vs $entry->getContent()->foo

(Asked by: username_0 on 2015-01-23)

I'm a bit confused about the usage of these two. Let's say I want to create a new entry from a controller. For me, the nicer option would be to user something like this:
$entry = new EntryModel();
$entry->sectionId = 4;
$entry->typeId = 4;

$entry->setContent([
    'foo' => 'bar',
    'xyz' => '456', 
]);

craft()->entries->saveEntry($entry);

Somehow this is not working for me. Instead, I have to use the uglier version like this:
$entry = new EntryModel();
$entry->sectionId = 4;
$entry->typeId = 4;

$entry->getContent()->foo = 'bar';
$entry->getContent()->xyz = '123';

craft()->entries->saveEntry($entry);

Any ideas why the first example might be failing? Especially when working with more than two attributes, the first example would be way more easy to handle.

**Tags:** entries, services

---

## 3 answers

### Answer by username_3 on 2015-01-25. Score: 13 | Accepted answer

You should be using [setContentFromPost()](http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail) rather than [setContent()](http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail), so that field types have a chance to prep the values that you are passing in.

See “[What is getContent() / the ContentModel, and how do I use it to get and set custom field values?](https://craftcms.stackexchange.com/q/4486/9)” for more info on how this stuff works.


---

### Answer by username_1 on 2015-01-24. Score: 6

I am just iterating on the previous answer to attempt to explain why this is happening. If you take a look at BaseElementModel.php at the setContent() method. You'll see that the core code is doing pretty much the exact thing as:
$entry->getContent()->setAttributes(array(
    'foo' => 'bar',
    'xyz' => '123'
));

The core confusion seems to be coming from a misunderstanding of how Yii models work. When a model as attributes defined, only those attributes can be set. Any other attribute will be ignored. I am not sure if you are trying to set attributes on a model that doesn't exist, but I used the following and it seems to work for me.
$entry = new EntryModel();
$entry->sectionId = 4;
$entry->typeId = 4;

// Works because title exists in the content model
$entry->setContent([
    'title' => 'bar'
]);

// foo and xyz do not exist in the content model, so attributes are ignored
$entry->setContent([
    'foo' => 'bar',
    'xyz' => '123'
]);

var_dump($entry->getContent()->getAttributes());exit();

The reason why the following works is because you using PHP to set a public property on an object instance. So if you are finding yourself having to rely on something like this, check to see if the attributes exist in the content model using var_dump or something similar.
$entry->getContent()->foo = 'bar';

You can see how the core method works here:
http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail

> **Comments:**
> **username_0**: thanks for the in-depth explanation. Obviously Brandons answer is also correct, not sure if I can accept two answers as correct?  
> **username_1**: I upvoted Brandon's answer :). I would probably go for his answer being correct since he's the creator of Craft and knows the platform a lot better than I do.  

---

### Answer by username_2 on 2015-01-23. Score: 4

Have you tried using that form of setting with this method instead?
$entry->setContentFromPost(['foo' => 'bar', 'xyz' => '123']);
There's also this method:
$entry->getContent()->setAttributes(array(
'foo' => 'bar',
'xyz' => '123'
));

As to the specific question of why doesn't setContent() work as expected, I can only guess that it may have to do with the values not being prepared properly.


---

