<p>I believe that <code>setContentFromPost</code> can only be used to set the content of custom fields on an <code>EntryModel</code>, not standard fields such as <code>title</code>.</p>

<p>Instead, you need to use <code>$entry-&gt;getContent()-&gt;title</code> to set the <code>title</code> on the <code>EntryModel</code>.</p>

<p>So your code would look like this:</p>

<pre><code>$movie = [
  'movieID'       =&gt; (int)$ctx_movie['MOVIE_ID'],
  'audioLanguage' =&gt; $ctx_movie['SPRACHVERSION'],
  'country'       =&gt; $ctx_movie['COUNTRY'],
  'director'      =&gt; $ctx_movie['DIRECTOR']
];

$entry = new EntryModel();
$entry-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle('movies')-&gt;id;
$entry-&gt;typeId    = craft()-&gt;sections-&gt;getSectionByHandle('movies')-&gt;type('movie')-&gt;id;
$entry-&gt;authorId  = craft()-&gt;userSession-&gt;getUser()-&gt;id;
$entry-&gt;enabled   = true;

// Set the title directly on the $entry
$entry-&gt;getContent()-&gt;title = $ctx_movie['VERANSTALTUNGSTITEL'];

// This should now work:
$entry-&gt;setContentFromPost($movie);

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

CC BY-SA 3.0
2016-10-24T13:13:03.630Z
Thanks, Steve! I actually tried this, but then it fails with an exception, as the title fields is read-only. It actually works to buypass that with `$entry->getContent()->title`, but from what I read, this is basically the hack workaround of setting variables that was discouraged, and it was recommeded to use `$entry->setContentFromPost()` instead. It's weird, then, that the mandatory `title` element can't be set neither directly nor via these functions.
KSP
CC BY-SA 3.0
2016-10-24T13:16:47.587Z
Yeah, you're right, you do need to use `$entry->getContent()->title` to set the title. I'll edit my answer to reflect this. It's still best to set the other fields using `setContentFromPost()`.
Steve Rowling
CC BY-SA 3.0
2016-10-24T13:26:56.977Z
Great, I've marked it as answered, and will use this in my code for now. Thanks! If anyone has any additional input as to why it is like that, I'd still be interested to learn more, or if there are alternative ways. Just seems weird that a mandatory field cannot use either of the two methods, and must use a method that is otherwise labeled as "hack" or "workaround".
KSP
CC BY-SA 3.0
2016-10-24T12:53:55.010Z
2016-10-24T13:18:35.690Z
2016-10-24T13:18:35.690Z
Steve Rowling
<p>I'm building a cinema program, and I'm trying to build entries for movies and shows loading external data via a plugin.</p>

<p>I'm getting close, but now I've hit a roadblock trying to save the entries. I build an array of the data to set as content that looks something like this:</p>

<pre><code>$movie = [
  'movieID'       =&gt; (int)$ctx_movie['MOVIE_ID'],
  'title'         =&gt; $ctx_movie['VERANSTALTUNGSTITEL'],
  'audioLanguage' =&gt; $ctx_movie['SPRACHVERSION'],
  'country'       =&gt; $ctx_movie['COUNTRY'],
  'director'      =&gt; $ctx_movie['DIRECTOR']
]
</code></pre>

<p>Then I make an entry model, and try to set the content to the above array:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId = craft()-&gt;sections-&gt;getSectionByHandle('movies')-&gt;id;
$entry-&gt;typeId    = craft()-&gt;sections-&gt;getSectionByHandle('movies')-&gt;type('movie')-&gt;id;
$entry-&gt;authorId  = craft()-&gt;userSession-&gt;getUser()-&gt;id;
$entry-&gt;enabled   = true;

// This doesn't work:
$entry-&gt;setContentFromPost($movie);

// This seems to work, creating an entry and populating fields:
// $entry-&gt;setContent($movie);

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

<p>I keep getting this error for every entry:</p>

<pre><code>array ( 'title' =&gt; array ( 0 =&gt; 'Name cannot be blank.', ), )
</code></pre>

<p>The value I set to the <code>'title'</code> element definitely has a value, and even if I hard code it to be a dummy string, it still fails in the same way.</p>

<p>It seems to work when using <code>setContent($movie)</code> instead of <code>setContentFromPost($movie)</code>, but from what I've read the latter is the correct (or best practice) way to do it.</p>

<p>I'm guessing it has something to do with the extra validation, but I can't for the life of me understand why a simple string like <code>'Dummy title'</code> fails the same way as my variable. The name <code>setContentFromPost</code> throws me off a bit, as I'm unsure if it means the data should come via a <code>post</code> request, but it seems to be fine with my other variables, and from the documentation they seem to be used in the same way, passing an array of values.</p>

<p>(On a side note, I got a similar error for a field that is of type dropdown. I guess here I just haven't understood what value to pass to a dropdown fieldType, so if someone has any tips here too, that would also be great, but I assume it may be two different issues.)</p>

<plugin-development><saveentry>
Problem saving new entry populated with setContentFromPost() while setContent() works
# Problem saving new entry populated with setContentFromPost() while setContent() works

(Asked by: username_0 on 2016-10-24)

I'm building a cinema program, and I'm trying to build entries for movies and shows loading external data via a plugin.
I'm getting close, but now I've hit a roadblock trying to save the entries. I build an array of the data to set as content that looks something like this:
$movie = [
  'movieID'       => (int)$ctx_movie['MOVIE_ID'],
  'title'         => $ctx_movie['VERANSTALTUNGSTITEL'],
  'audioLanguage' => $ctx_movie['SPRACHVERSION'],
  'country'       => $ctx_movie['COUNTRY'],
  'director'      => $ctx_movie['DIRECTOR']
]

Then I make an entry model, and try to set the content to the above array:
$entry = new EntryModel();
$entry->sectionId = craft()->sections->getSectionByHandle('movies')->id;
$entry->typeId    = craft()->sections->getSectionByHandle('movies')->type('movie')->id;
$entry->authorId  = craft()->userSession->getUser()->id;
$entry->enabled   = true;

// This doesn't work:
$entry->setContentFromPost($movie);

// This seems to work, creating an entry and populating fields:
// $entry->setContent($movie);

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

I keep getting this error for every entry:
array ( 'title' => array ( 0 => 'Name cannot be blank.', ), )

The value I set to the 'title' element definitely has a value, and even if I hard code it to be a dummy string, it still fails in the same way.
It seems to work when using setContent($movie) instead of setContentFromPost($movie), but from what I've read the latter is the correct (or best practice) way to do it.
I'm guessing it has something to do with the extra validation, but I can't for the life of me understand why a simple string like 'Dummy title' fails the same way as my variable. The name setContentFromPost throws me off a bit, as I'm unsure if it means the data should come via a post request, but it seems to be fine with my other variables, and from the documentation they seem to be used in the same way, passing an array of values.
(On a side note, I got a similar error for a field that is of type dropdown. I guess here I just haven't understood what value to pass to a dropdown fieldType, so if someone has any tips here too, that would also be great, but I assume it may be two different issues.)

**Tags:** plugin-development, saveentry


---

## 1 answers

### Answer by username_1 on 2016-10-24. Score: 6 | Accepted answer

I believe that setContentFromPost can only be used to set the content of custom fields on an EntryModel, not standard fields such as title.
Instead, you need to use $entry->getContent()->title to set the title on the EntryModel.
So your code would look like this:
$movie = [
  'movieID'       => (int)$ctx_movie['MOVIE_ID'],
  'audioLanguage' => $ctx_movie['SPRACHVERSION'],
  'country'       => $ctx_movie['COUNTRY'],
  'director'      => $ctx_movie['DIRECTOR']
];

$entry = new EntryModel();
$entry->sectionId = craft()->sections->getSectionByHandle('movies')->id;
$entry->typeId    = craft()->sections->getSectionByHandle('movies')->type('movie')->id;
$entry->authorId  = craft()->userSession->getUser()->id;
$entry->enabled   = true;

// Set the title directly on the $entry
$entry->getContent()->title = $ctx_movie['VERANSTALTUNGSTITEL'];

// This should now work:
$entry->setContentFromPost($movie);

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

> **Comments:**
> **username_0**: Thanks, Steve! I actually tried this, but then it fails with an exception, as the title fields is read-only. It actually works to buypass that with `$entry->getContent()->title`, but from what I read, this is basically the hack workaround of setting variables that was discouraged, and it was recommeded to use `$entry->setContentFromPost()` instead. It's weird, then, that the mandatory `title` element can't be set neither directly nor via these functions.  
> **username_1**: Yeah, you're right, you do need to use `$entry->getContent()->title` to set the title. I'll edit my answer to reflect this. It's still best to set the other fields using `setContentFromPost()`.  
> **username_0**: Great, I've marked it as answered, and will use this in my code for now. Thanks! If anyone has any additional input as to why it is like that, I'd still be interested to learn more, or if there are alternative ways. Just seems weird that a mandatory field cannot use either of the two methods, and must use a method that is otherwise labeled as "hack" or "workaround".  

---

