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