Creating two entities at once using entity metadata wrappers I'm currently facing a situation where I need to create a new instance of a certain entity and attach a file to it.

Since the main entity should 'reference' the file (the same way a node references a user, by an entity property) there are several things I can do.

Method 1


Save the main entity
Save the file
Update the main entity with the file id.


Method 2


Save the file with temporary status
Save the main entity with the file id.
Update the file to a permanent status.


Since both methods would require me to perform 3 operations in a row I thought it might be simpler to do this with an entity metadata wrapper like this:

// Create the file entity.
$f = entity_create('file', array(
  'uid'               =&gt; $data-&gt;uid,
  'filename'          =&gt; $data-&gt;filename,
  'url'               =&gt; $data-&gt;uri,
  'filemime'          =&gt; $data-&gt;filemime,
  'filesize'          =&gt; $data-&gt;filesize,
  'status'            =&gt; 1,
));


// Create the main entity.
$e = entity_create('main_entity', array());
$entity= entity_metadata_wrapper('main_entity', $e);

$entity-&gt;title       = $data-&gt;title;
$entity-&gt;...         = $data-&gt;...
$entity-&gt;file        = $f;

$entity-&gt;save();


This however saves the main entity with $entity-&gt;file = 0, and the file does not show up in file_managed. Which conflicts with the answer to "How to set value of file field with entity_metadata_wrapper" that implies I can use a new stdClass().

Is there some other error in my entity metadata implementation, or is it not possible to save (create) two entities at once like this?

Update

Apparently my understanding on entity metadata wrappers was completely wrong; While reading the comments on the Entity metadata wrappers documentation page I assumed I could do things like this:

// Changing the author's email.
$wrapped_node-&gt;author-&gt;mail-&gt;set('foo@bar.baz');
// Adding to the answer of @Zakaria Elhariri.
$wrapped_node-&gt;field_page-&gt;status-&gt;set(0);
// Save the changes.
$wrapped_node-&gt;save();


I thought saving the main entity would update the nested entities, so I'd expect the email of my account to be changed and the linked node of Zakaria Elhariri's answer to be unpublished. But none of this is true. Why is this is the examples?


  To update the user's mail address one could use
  
  &lt;?php 
  $wrapper-&gt;author-&gt;mail-&gt;set('sepp@example.com'); 
  ?&gt;


Or is entity metadata wrapper broken somehow?
Without saving the first entity i dont think its possible.

This is KO :

  // Create the first entity.
  $page = entity_create('node', array('type'=&gt;'page'));
  $entity_page = entity_metadata_wrapper('node', $page);
  $entity_page-&gt;title = 'page test!';
  //$entity_page-&gt;save();

  // Create the second entity.
  $article = entity_create('node', array('type'=&gt;'article'));
  $entity= entity_metadata_wrapper('node', $article);
  $entity-&gt;title = 'article without reference KO';
  $entity-&gt;field_paage = $paage;
  $entity-&gt;save();


This is OK : 

  // Create the first entity.
  $page = entity_create('node', array('type'=&gt;'page'));
  $entity_page = entity_metadata_wrapper('node', $page);
  $entity_page-&gt;title = 'page test!';
  $entity_page-&gt;save();

  // Create the second entity.
  $article = entity_create('node', array('type'=&gt;'article'));
  $entity= entity_metadata_wrapper('node', $article);
  $entity-&gt;title = 'article with reference OK';
  $entity-&gt;field_paage = $paage;
  $entity-&gt;save();


with the second code you have 2 entity (article and page) with the first node page is a reference for the content article.

What you need is save in the first time your file object.

To Update some fields (tested) :  

  $wrapper = entity_metadata_wrapper('user', $user);
  $wrapper-&gt;mail-&gt;set('sepp@example.com'); 
  $wrapper-&gt;field_first_name-&gt;set('First name');
  $wrapper-&gt;save();