Q:

How do I programmatically set a taxonomy term for a node?

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)

A:

In Drupal 7, taxonomy reference fields existed which used the pattern ['tid' => NUMBER]. These are entity reference fields. The EntityReferenceItem has the property definitions: target_id and entity. The former is a either an integer or string depending on the entity reference item's settings (basically config/content entity type).
This is also a similar question to How to set value for multivalue field in drupal 8 programmatically
So try to use the pattern
As part of Entity::create
Node::create([
  'field_meldungstyp' => [['target_id' => 19]]
]);

Using FieldableEntityInterface::set
$entity->set('field_meldungstyp', ['target_id' => 19]);

Using TypedDataInterface::setValue on the field item directly.
$entity->field_meldungstyp->setValue(['target_id' => 19]);

With magic method
$entity->field_meldungstyp->target_id = 19;

Note that this overwrites any current values of the field.

A:

It needs to be a nested array.
'field_meldungstyp'  => [
      ['target_id' => 12345]
    ]

Try this:
$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_meldungstyp'  => [
      ['target_id' => 12345]
    ]
));
$node->save();

A:

Here is how i did it for D8.
use Drupal\node\Entity\Node;

//To create new node
$node = Node::create(['type' => 'YOUR_CONTENT_TYPE_HERE']);

//To load a node by its ID
//$node = \Drupal\node\Entity\Node::load(NODE_ID);

$nodeTerms = [1,2,3...]; // 1,2,3 = terms IDs
$node->set('YOUR_FIELD_OF_TERMS', $nodeTerms);
$node->save();

//To create new tax
use Drupal\taxonomy\Entity\Term;

$new_term = Term::create([
    'vid' => "YOUR_VOCABULARY_MACHINE_NAME",
    'name' => "NAME_OF_YOUR_TERM",
]);
$new_term->save();

