Closed (fixed)
Project:
Replicate
Version:
7.x-1.0
Component:
Code
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
9 Jan 2014 at 23:44 UTC
Updated:
29 Jan 2014 at 18:30 UTC
Jump to comment: Most recent
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
Comment #1
cupcakemuncher commentedHi 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
Comment #2
koppie commented@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
Comment #3
cupcakemuncher commentedOh 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.
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
Comment #4
jangolden commented@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!
Comment #5
cupcakemuncher commentedHi 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
Comment #6
koppie commentedI'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:
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!