1

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'               => $data->uid,
  'filename'          => $data->filename,
  'url'               => $data->uri,
  'filemime'          => $data->filemime,
  'filesize'          => $data->filesize,
  'status'            => 1,
));


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

$entity->title       = $data->title;
$entity->...         = $data->...
$entity->file        = $f;

$entity->save();

This however saves the main entity with $entity->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->author->mail->set('foo@bar.baz');
// Adding to the answer of @Zakaria Elhariri.
$wrapped_node->field_page->status->set(0);
// Save the changes.
$wrapped_node->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

<?php $wrapper->author->mail->set('sepp@example.com'); ?>

Or is entity metadata wrapper broken somehow?

2
+50

Without saving the first entity i dont think its possible.

This is KO :

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

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

This is OK :

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

  // Create the second entity.
  $article = entity_create('node', array('type'=>'article'));
  $entity= entity_metadata_wrapper('node', $article);
  $entity->title = 'article with reference OK';
  $entity->field_paage = $paage;
  $entity->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->mail->set('sepp@example.com'); 
  $wrapper->field_first_name->set('First name');
  $wrapper->save();        
  • Thanks for taking the time to respond. It is a lot easier testing it like this indeed and it seems you are right and it is not possible. I'd like to keep the question open for a little longer though in case somebody posts a smart workaround of some sort :) – Neograph734 Dec 9 '15 at 11:31
  • Could you perhaps take a look at my updated question and share your thoughts? – Neograph734 Dec 9 '15 at 12:05
  • I update my answer, to update some fields with wrapper... – Zakaria Elhariri Dec 10 '15 at 8:24
  • Thanks, I figured this works (basically you are setting the fields on the entity you wrapped). My question is specifically about setting values on a 2nd level entity. Like setting the email of the author of the node (see my update). The linked documentation uses this as an example, but also the author is not saved when I save the node (the documentation implies this would work, hence the confusion for my initial question). – Neograph734 Dec 10 '15 at 13:12
  • Basically I'd like to know if it is my system conflicting with something or if entity metadata wrappers by design do not save 2nd level entities even though I can alter them. – Neograph734 Dec 10 '15 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.