How can I programmatically create a media object and a new file and associate the file with the media object? I am trying to programmatically create a media object and a file, and then associate the file with the media object.  The first part works--I can successfully create the media object and file.  But the second part fails--the media object has no file associated with it.  

Here is my code:

//Begin handling file uploads
$file = file_save_data('hey dude', 'public://2019-07/my_example.txt', FILE_EXISTS_REPLACE);

$media_arr = [
  'bundle' =&gt; 'file',
  'langcode' =&gt; 'en',
  'uid' =&gt; '0',
  'name'=&gt; 'Test Name',
  'created' =&gt; date('U'),
  'changed' =&gt; date('U'),      
  'moderation_state' =&gt; 'published',
  'file' =&gt; [
    'target_id' =&gt; $file-&gt;id(),
  ],
];

$drupalMedia = Media::create($media_arr);

$drupalMedia-&gt;setName('Test Media Object')
  -&gt;setPublished(TRUE)
  -&gt;save();   


my_example.txt is created in the files directory, and the media object "Test Media Object" is also created, and I'm able to attach it to a node.  But there is no file in the new media object!  Am I missing something here?
Ah, I always figure it out after I say I can't do it.  Once I specified the document type, it worked: 

$media_arr = [
  'bundle' =&gt; 'file',
  'langcode' =&gt; 'en',
  'uid' =&gt; \Drupal::currentUser()-&gt;id(),
  'created' =&gt; date('U'),
  'changed' =&gt; date('U'),      
  'moderation_state' =&gt; 'published',
  'field_file_document_type' =&gt; 1409, //1409 is the id for pdf
  'field_media_file' =&gt; [
    'target_id' =&gt; $file-&gt;id(),
  ],
];


Edit: I should mention that 1409 is the id we use for pdfs, yours will be different.  The field_file_document_type just needed to have a value in it to work.