Saving of bean is too slow and not complete

Hello,

I want to save a bean object in while loop but the saving is too slow and it’s not complete, it saves just half, I don’t understand. I have 2017 recordings to save.

Someone can help me ?? :frowning: :frowning: :frowning:

This is my code :

while($row=$db->fetchByAssoc($result))
{
$rel_emp =new FOI_Emplacements_Evenements();
$rel_emp->name=$row[“nom_emp”]."-".$bean->name;
$rel_emp->foi_evenements_foi_emplacements_evenements_3foi_evenements_ida=$bean->id;
$rel_emp->foi_evenem68b8nements_idb=$row[“id”];
$rel_emp->categorie_c=“normal”;
$rel_emp->disponibilite_c=“03”;
$rel_emp->rue_c=$row[“nom_voie”];
$rel_emp->save();
//unset($rel_emp);
}

It’s hard to say what’s going on without more information. Are there any logic hooks or custom logic for the bean?

Saving 2017 records will take a bit of time, it’s possible that the PHP timeout is being reached. You could possibly set the limit in each loop:
http://php.net/manual/en/function.set-time-limit.php

An alternative would be to check for existing records and skip those which have already been saved so that, at least, multiple runs will save more of the records.

Hope this helps,
Jim

I have /custom/modules/FOI_Evenements/logic_hooks.php file with an after_save logic hook :


<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will	
// be automatically rebuilt in the future. 
 $hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(1, 'Creation Emplacement', 'custom/modules/FOI_Evenements/hook.php','Evenement', 'creationEmp'); 

?>

and the hook.php file with this part of code :



while($row=$db->fetchByAssoc($result))
{
$rel_emp =new FOI_Emplacements_Evenements();
$rel_emp->name=$row["nom_emp"]."-".$bean->name;
$rel_emp->foi_evenements_foi_emplacements_evenements_3foi_evenements_ida=$bean->id;
$rel_emp->foi_evenem68b8nements_idb=$row["id"];
$rel_emp->categorie_c="normal";
$rel_emp->disponibilite_c="03";
$rel_emp->rue_c=$row["nom_voie"];
$rel_emp->save();
//unset($rel_emp);
} 

this code is supposed to create a new bean FOI_Emplacements_Evenements. and there is 2071 Emplacements_Evenements, but it saved just half of 2071, I don’t understand why it stopped ??

I change the set_limit_time, but there is the same problem.

To confirm you want to save 2071 new records everytime a FOI_Evenements is created or edited?

Long running actions generally shouldn’t go into logic hooks. See here for information about adding a task to the job queue: http://developer.sugarcrm.com/2012/08/09/use-the-new-job-queue-to-make-logic-hooks-perform-faster/

Saving Beans tends to be slow and timeout

Have you considered changing your code to use an sql statement instead?

This tends to be much quicker and you can often write the sql so that it modifies multiple records in one go