This question is a follow up of a previous question see: (Entry Form - Multiple Entries & Entry Type & Parent)
I am trying to add multiple children to a pre-existing multi level structure:
Level 1
--Level 2 (The parent where I am adding the children)
----Child 1
----Child 2
..etc
My Controller:
public function actionSaveAnimals(){
$this->requirePostRequest();
if($rodentsData = craft()->request->getParam('rodent')){
$rodentErrors = [];
foreach ($rodentsData as $rodentData){
if (array_key_exists('entryId', $rodentData) && $rodentId = $rodentData['entryId']) {
if (!$rodent = craft()->elements->getElementById($rodentId)) {
throw new Exception('Could not find rodent with the given Id');
}
} else {
$rodent = new EntryModel();
$rodent->typeId = 6; // change your entryType Id here
$rodent->sectionId = craft()->request->getParam('sectionId');
$rodent->parentId = craft()->request->getParam('parentId');
$rodent->authorId = craft()->request->getParam('authorId');
}
$rodent->getContent()->setAttributes($rodentData['fields']);
if (!craft()->entries->saveEntry($rodent)) {
$rodentErrors[] = $rodent->getErrors();
}else{
$rodentErrors = [];
}
}
// include errors of each item
craft()->urlManager->setRouteVariables(array(
'rodentErrors' => $rodentErrors
));
}
}
And my form:
<form method="post" accept-charset="UTF-8">
{{ getCsrfInput() }}
{# your plugin name and function name #}
<input type="hidden" name="action" value="experiment/SaveAnimals">
{# the id of your section for me its 4 you need to change this value to your section Id #}
<input type="hidden" name="sectionId" value="4">
{# enable the entry #}
<input type="hidden" name="enabled" value="1">
{# id of your entry leave it blank to insert a new entry, insert a id to change an existing entry#}
<input type="hidden" name="entryId" value="">
<input type="hidden" name="parentId" value="{{ entry.id }}">
<input type="hidden" name="authorId" value="{{ currentUser.id }}">
<div class="fields">
<div class="field mt3">
<label class="f5 lh-copy pb2" for="title">Amount of Animals</label>
{#<input class="pa3 input-reset ba bw0 br2 w-100" type="number" id="Cage__Amount" placeholder="5" max="10" required>#}
<input type="text" name="rodent[0][fields][title]" value="Add Identifier">
<input type="text" name="rodent[1][fields][title]" value="Add Identifier">
</div>
</div>
<div class="field submit mv4">
<button class="bg-dark-blue white ph3 pv2 input-reset pointer ba bw0 f5 dib lh-copy w-100" type="submit">Create experiment</button>
</div>
I am currently getting this error:
Trying to get property of non-object
From EntriesService.php(468)
if ($entry->getSection()->type != SectionType::Structure)
Any help much appreciated!
var_dump($rodent->getSection());die();
right before you save the entry and tell me if your result is something likeobject(Craft\SectionModel)
if not then please just include a$section = craft()->sections->getSectionById(4)
right at the start of your controller, and check if$section->handle
is your correct handle. My guess is: your ids are wrong – Robin Schambach Dec 2 '17 at 19:21