Incorrect data after updating via php

Formidable Forms
14 December, 2021 02:31:52
Merlin1987
Topics: 3
Messages: 8
Hello,

i have the problem that e2pdf takes old or cached data from an entry.

I have entries in a form wich i updated via php (with FrmEntryMeta::update_entry_meta( $result->id, 292, '', $platz );) and i'm updating the "updated_at" meta too. But the download shortcode [e2pdf-download id="16" dataset="[id]" name="MOUNTAINMAN_Urkunde_[146]_[147]" button-title="Download Urkunde »" class="pdf-button"] takes old data and not the data from the updated entry. When i go to the entry, it shows the correct data and i have to update the entry manually (with same data) to get the right data to the pdf. Is there a way to clear a e2pdf case or something like that?

Thanks in advance,
Merlin
14 December, 2021 04:22:11
E2Pdf
Support
Topics: 7
Messages: 3346
Hi,

E2Pdf uses Formidable Forms render engine to output data so can you try please to add after your code which updates entry:

FrmEntry::clear_cache();

It must clear Formidable Forms cache.
We would really appreciate your feedback at WordPress.org!
14 December, 2021 04:44:23
Merlin1987
Topics: 3
Messages: 8
Hi,

sorry, no. Still the same. I add the code after i update the field but nothing changed, when i loop again through the entries. Is there a way to see if the formidable cache is really cleared?

To specify, i have a form where the user can enter running (sport) results and after any entry i loop through any result and calculate the ranking and update it in the entry. The view and anything else looks good and shows the right values. Only the pdf is wrong. The pdf is only correct, when i manually go in an entry and hit the update button. After that the pdf changed correctly for that entry. I have no caching plugins active and caring in e2pdf is disabled.

This is full code if it helps:

function calculate_ranking( $track_id, $distance = '' ) {

// Get all results by track-id
$form_id = 11; // FormID 4 = 4 | Ergebnisse Teilnehmer

$s_query = array('it.form_id' => $form_id);

if (is_callable('FrmProEntriesHelper::get_search_str')) {

$s = $track_id;
$field_id = 129;

}

$s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $field_id ); // FrmProEntriesHelper::get_search_str( $s_query, $s, $form->id, $field_id )

$all_results = FrmEntry::getAll($s_query, ' ORDER BY it.created_at ASC', '', true);

foreach ( $all_results as $result ) {

// Double check user ID
if( $result->metas[129] == $track_id ) { // 129 = Track_ID

if ( empty($distance) || (!empty($distance) && $distance == $result->metas[286]) ) { // 286 = Streckenlänge

// Platz Gesamt KM
$platz_g = array();
$platz_g['id'] = 138;
$platz_g['type'] = 'count';
$platz_g[288] = 'Freigegeben'; // Status
$platz_g[129] = $track_id; // TrackID
$platz_g[286] = $result->metas[286]; // Streckenlänge
$platz_g['132_less_than'] = $result->metas[132]; // Zeit in Sekunden

$platz = FrmProStatisticsController::stats_shortcode($platz_g);

$platz++;

$added = FrmEntryMeta::add_entry_meta( $result->id, 291, '', $platz ); // 291 = Platz Gesamt KM
if ( ! $added ) {
FrmEntryMeta::update_entry_meta( $result->id, 291, '', $platz );
}

// Platz M/W KM
$platz_g = array();
$platz_g['id'] = 138;
$platz_g['type'] = 'count';
$platz_g[288] = 'Freigegeben'; // Status
$platz_g[129] = $track_id; // TrackID
$platz_g[286] = $result->metas[286]; // Streckenlänge
$platz_g[145] = $result->metas[145]; // Geschlecht
$platz_g['132_less_than'] = $result->metas[132]; // Zeit in Sekunden

$platz = FrmProStatisticsController::stats_shortcode($platz_g);

$platz++;

$added = FrmEntryMeta::add_entry_meta( $result->id, 292, '', $platz ); // 291 = Platz Gesamt KM
if ( ! $added ) {
FrmEntryMeta::update_entry_meta( $result->id, 292, '', $platz );
}

global $wpdb;
$new_values = array(
'updated_by' => 2,
'updated_at' => current_time( 'mysql', 1 ),
);
$query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, array( 'id' => $result->id ) );

FrmEntry::clear_cache();

}
}
}


}
14 December, 2021 04:47:45
Merlin1987
Topics: 3
Messages: 8
292 is the field that's wrong on the pdf.
14 December, 2021 07:34:47
E2Pdf
Support
Topics: 7
Messages: 3346
We currently could replicate an issue and it's not connected with "cache". It looks it's bug in code + Formidable Forms way render.

When you use the code:


$added = FrmEntryMeta::add_entry_meta( $result->id, 292, '', $platz ); // 291 = Platz Gesamt KM
if ( ! $added ) {
FrmEntryMeta::update_entry_meta( $result->id, 292, '', $platz );
}


It always adds new entry to database with same field identifier which must not be like this as by default it must be updated and not added. In database it looks like on 1st screenshot attached. It's multiple updates with your code. When Formidable Forms renders the shortcodes inside PDF / Views / Success Messages - it uses 1st occurrence from database and as it's not updated you see old output. Formidable Forms doesn't have that issue when you edit / view entry only when shortcodes like [123] used.

When you use "update" function from Formidable Forms backend - it updates multiple entries for same field and that's why you see correct value in PDF (2nd screenshot) after that action.

So the possible solution can be:

1.
1.1 Correct "duplicated" entries inside database. It's important as it will update last occurrence and you will still get old value inside PDF.
1.2. Update your code to detect if value exists firstly and use FrmEntryMeta::update_entry_meta( $result->id, 292, '', $platz ); if exists.

As:

$entry = FrmEntry::getOne($result->id, true);
if (isset($entry->metas['292'])) {
FrmEntryMeta::update_entry_meta($result->id, 292, '', $platz);
} else {
FrmEntryMeta::add_entry_meta($result->id, 292, '', $platz);
}


Or

2. Use another code for update to do action as with "backend" update:


$entry = FrmEntry::getOne($result->id, true);
if (isset($entry->metas)) {
$metas = $entry->metas;
$metas['292'] = $platz; // new value
FrmEntryMeta::update_entry_metas($result->id, $metas);
}


P.S. Keep in mind please that this is just code examples and you will need to test and correct, add addtional checks (if needed) before implementation to live website.

P.P.S. Unfortunately we can't correct that issue as it's connected with Formidable Forms core. We will try to contact and report them about the issue.

We remain at your service.
We would really appreciate your feedback at WordPress.org!
14 December, 2021 12:25:47
Merlin1987
Topics: 3
Messages: 8
Hello,

awesome. Thank you. I've implemented your first suggestion and it worked perfectly.