Hi,
I’d like to generate invoices from a hook
and I got a php memory limit error. I don’t want to use the simple-but-sad solution of increasing the limit in my php.ini
.
I got no problem to fetch all the invoice from my external api and store them into a beautiful array()
, let’s call it $invoices
.
I loop through each invoice in my $invoices
array but even if I call $db->commit()
after each loop, the php buffer seems to increase.
My code (simplified) :
$invoices = array(...); // My array with all pre-fetched invoices
foreach ($invoices as $facture) { // Main loop
$new_invoice = new Facture($db) // Invoice object creation
[...] // Differents attributes of my $new_invoice object (Facture)
foreach ($facture['lines'] as $line) {
$line = new FactureLigne($db);
[...] // Différents attributes de my $line object (FactureLigne)
}
$id = $new_invoice->create($user); // Invoice creation (before payment)
foreach ($facture['payments'] as $payment) {
$new_payment = new Paiement($db);
$new_payment->amount = $payment['amount'];
$new_payment->amounts = array();
$new_payment->amounts[$id] = $new_payment->amount; // facture <-> paiement link
[...] // Differents attributes of my $new_payment object (Paiement)
}
if ($id < 0) {
$error++;
dol_print_error($db, $new_invoice->error);
} else {
$new_invoice->validate($user);
dol_syslog("Invoice created with id=" . $id . "\n", LOG_INFO);
$db->commit(); // ---> HERE <----
}
}
When I run this code and I look into the size of $db->queries
when I try to commit (on the ---> HERE <---
comment), it seems to increase on each loop iteration of my main loop without purge.
How to modify / optimize this script to unload memory on each iteration?
Thanks for your help and sorry for my english.
Lucien