I hate creating an issue with a stupid title like that, but I can't get the module to work at all. Here's my code:

    $selected = $form_state['build_info']['args'][0]->field_template['und'][0]['target_id'];
    $entity = entity_load('routing_template', array($selected));
    $values = array(
      'type'              => "routing_template",
      'title'             => "Copy of " . $entity[$selected]->title,
      'field_istemplate'  => array('und' => array(0 => array ('value' => 0))),
      'rdf_mapping'       => array(),
    );
    $entity_type = 'routing_template';
    $replicant = replicate_entity_by_id($entity_type, $selected);

Here's the error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5' for key 'PRIMARY': INSERT INTO {eck_routing_template} (id, type, title) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 5 [:db_insert_placeholder_1] => routing_template [:db_insert_placeholder_2] => Template 2 ) in drupal_write_record() (line 7170 of /var/www/html/sfo_dts/includes/common.inc).

Using Drupal 7.24 with Entity API 1.3. Any idea why it won't work?

Comments

cupcakemuncher’s picture

Hi there!
The code you gave us shows a formarray.
The errormessage describes an exception, hinting towards a violation concerning databaseintegrity.
So somewhere along the line you call drupal_write_record() or have it called.
Because of the following snipped
...Duplicate entry '5' for key 'PRIMARY': INS... I assume you try to enter a second entry into the database baring an already existing primarykey.
cheers

cupcakemuncher

koppie’s picture

@Cupcakemuncher thanks for the reply! But, see, that's the whole point of the module: to replicate an entity. I assumed I could call the function and it would replicate my entity. Instead it gives me a duplicate error. Shouldn't it create a new entity for me? I'm not writing to the database anywhere else. #confused

cupcakemuncher’s picture

Oh sorry,
yeah the last line, I completly overlooked.
Was a tough day yesterday.
Well....
Lucky you that PHP is a script-language.
Looking at replicate.module the function where all the magic happens is replicate_clone_entity() .
From the following code-snippet I take that you must implement a hook to get it working.

....
  if ($clone) {
    // Let other modules manage the cleaning of the entity.
    foreach (module_implements('replicate_entity_' . $entity_type) as $module) {
      $function = $module . '_replicate_entity_' . $entity_type;
      $function($clone);
    }
....

A look at replicate.api.php reveals that in deed there is a hook going by that name hook_replicate_entity_ENTITY_TYPE() PHPDocs will be of greate help
replicate_entity() will call entity_save() further down the road.

BTW: when I clone an entity I also clone data, which is designated to be their primary key in the database, thus violating constraints when one tries to save it to the database, so it behaves as expected.

cheers

cupcakemuncher

jangolden’s picture

@cupcakemuncher

Thanks for your prompt replies to this issue. I can't use this module at all since I get the duplicate error also in every instance.

Can you provide a patch to the module to get around this issue?

To clarify the problem...when trying to 'replicate' entity with an id of 1, the replicate module tries to insert id 1 again. Shouldn't it increment the entity id?

Any insight or workaround would be appreciated. Thanks!

cupcakemuncher’s picture

Hi there,

AFAIK you are responsible to define how to process the data.
I would set the id to null, thus allowing Drupal to set the id.
All other informations are already written in my post above.
cheers

koppie’s picture

Status: Active » Closed (fixed)

I'm embarrassed to say it actually does work. I got a fresh pair of eyes to look at it and it turns out the entity table has been updated the whole time. Here's my code:

    $entity = entity_load('routing_template', array($selected)); // $selected is id of entity to be cloned
    $values = array(
      'type'              => "routing_template",
      'title'             => "Copy of " . $entity[$selected]->title,
      'field_istemplate'  => array('und' => array(0 => array ('value' => 0))),
      'rdf_mapping'       => array(),
    );
    $replicant = entity_create('routing_template', $values);
    $replicated = entity_save('routing_template', $replicant);
    
    dpm($replicated, 'replication result'); // returns "1" for positive result

I still need to clone the field collection inside the entity, but that's a FC issue. I think I'm good here. :-) I'm going to close this ticket now. Thanks for your help @cupcakemuncher!