I got some difficulties setting a taxonomy for a node. Currently I am using this code.
$node = Node::create(array(
'type' => 'detailseite',
'title' => $data[$headlineIndex],
'langcode' => 'de',
'uid' => '1',
'status' => 1,
'field_headline' => $data[$headlineIndex],
'field_intro' => $data[$introIndex] . $categories[$data[$categoryIndex]],
'created' => $data[$dateIndex],
'field_autor' => $data[$autorIndex],
'field_teaser_text' => $data[$shortIndex],
'field_fields' => array(),
));
$node->save();
Which works fine so far (below an image of the fields of "detailseite").
Everything gets set correctly and I can find the newly created nodes in the admin area that work correctly. Now I need to set 'field_meldungstyp' which is an Entity-Reference to a Taxonomy:
which already contains three entries:
I want to get the node(s) I created programmatically to have "field_meldungstyp" set to "Artikel" of the "Meldungstyp"-Vocabulary.
I already tried adding:
'field_meldungstyp' => array( 'tid' => 19 ),
where 19 is the tid of "Artikel" of the earlier mentioned vocabulary. And that's where I'm stuck. Also https ://www.drupal.org/node/2402811 didn't work for me. As well as
$node->field_meldungstyp[$node->language][]['tid'] = 19;
didn't work either.
simply adding
$node = Node::create(array(
...
'field_meldungstyp' => 19,
...
));
results in crashing with the following error:
Drupal\Core\Entity\EntityStorageException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'datetime' for column 'created' at row 1: INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => 343 [:db_insert_placeholder_1] => 19 [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => datetime ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). Drupal\Core\Database\Statement->execute(Array, Array) (Line: 610) Drupal\Core\Database\Connection->query('INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4)', Array, Array) (Line: 81) Drupal\Core\Database\Driver\mysql\Connection->query('INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4)', Array, Array) (Line: 32) Drupal\Core\Database\Driver\mysql\Insert->execute() (Line: 371) Drupal\Core\Database\Query\Merge->execute() (Line: 525) taxonomy_build_node_index(Object) (Line: 479) taxonomy_node_insert(Object) call_user_func_array('taxonomy_node_insert', Array) (Line: 402) Drupal\Core\Extension\ModuleHandler->invokeAll('node_insert', Array) (Line: 167) Drupal\Core\Entity\EntityStorageBase->invokeHook('insert', Object) (Line: 418) Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('insert', Object) (Line: 470) Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, ) (Line: 304) Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, ) (Line: 395) Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 761) Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 364) Drupal\Core\Entity\Entity->save() (Line: 46) Drupal\awo_importer\Controller\T3ImporterController->import() call_user_func_array(Array, Array) (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 574) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() call_user_func_array(Object, Array) (Line: 144) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)