2

I want to clone an entity for multiple users, But some fields for each of them is different, Some fields are the same.

One way is to create the Entity for each:

// Doing in this in a loop for each user.
$e = entity_create('type', $info);
$e->{$same_field_for_all1}->set($value1);
$e->{$same_field_for_all2}->set($value2);
$e->{$same_field_for_all3}->set($value3);
$e->{$same_field_for_all4}->set($value4);
$e->{$fieldName}->set($this->getUserValue($user1, $fieldName));
// Set author and title for user here...
$e->save();

Another way is:

// Doing this only once
$e entity_create('type', $info);
$e->{$same_field_for_all1}->set($value1);
$e->{$same_field_for_all2}->set($value2);
$e->{$same_field_for_all3}->set($value3);
$e->{$same_field_for_all4}->set($value4);

// And doing this in a loop:
$clone = clone $e;
$clone->{$fieldName}->set($this->getUserValue($currntUser, $fieldName));
// Set author and title for user here...
$clone->save();

Is this sort of shallow cloning of EntityMetadataWrapper safe?

2
  • Is it possible that your $info has something common in it like nid? And is 'type' a custom entity of yours, or is that just a typo in your question and maybe should be 'node' as the first param to entity_metadata_wrapper()? – texas-bronius Oct 29 '15 at 21:30
  • @texas-bronius well it was a long time ago. I really don't remember. – hkoosha Nov 4 '15 at 19:35
0

Well, It's not safe/ I don't know why yet, But, Sometimes it result in saving only last cloned entity.

It's not a good answer till it says why!

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.