• Resolved jamminjames

    (@jamminjames)


    When using the “Forminator_API::update_form_entry” method, how do you update items that are not simple “name”/”value” arrays?

    For example, a simple field has an array like:

    [email-1] => Array ( [id] => 67 [value] => [email protected] ) 

    …and can be updated using entry meta like:

    $entry_meta = array(
       array(
          'name' => $key,
          'value' => $value
       )
    );

    But what about a field that has a deeper array, like:

    [name-1] => Array ( [id] => 68 [value] => Array ( [first-name] => john [last-name] => testy ) )

    How do you construct the entry meta for such a field, so that it may be updated? Thanks for any help.

    • This topic was modified 1 year, 2 months ago by jamminjames.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jamminjames

    Thank you for your question!

    To update such data you simply need to serialize it.

    For example:

    $entry_meta= array(
    	array(
    		'name' => 'name-1',
    		'value' => maybe_serialize( array( 'first-name' => 'test first', 'last-name' => 'test last' ) )
    	)
    );

    I hope that helps!

    Best regards,
    Adam

    Thread Starter jamminjames

    (@jamminjames)

    Thanks. However, I’m having trouble if there is more than one field to update, and if one needs to be serialized and one doesn’t. I’ve tried two ways, neither is working. With the first, it only updates the serialized field, with the second, it only updates the non-serialized, and messes up the form data for the serialized field. It’s the same result if I take out the “[]” brackets, which it seem like they’re needed, since we need to add to the array for the multiple field updates:

    foreach ($ids_new_arr as $k => $d) {
      if (is_array($d)) {
        $entry_meta[] = array(
          array(
            'name' => $k,
            'value' => maybe_serialize( array( $d ) )
          )
        );
      } else {
        $entry_meta[] = array(
          array(
            'name' => $k,
            'value' => $d
          )
         );
      }
    }

    and

    foreach ($ids_new_arr as $k => $d) {
      if (is_array($d)) {
         $entry_meta[] = array(
    	'name' => $k,
    	'value' => maybe_serialize( array( $d ) )
         );
      } else {
         $entry_meta[] = array(
    	'name' => $k,
    	'value' => $d
         );
      }
    }
    Thread Starter jamminjames

    (@jamminjames)

    Okay, I think this is better, but it’s still not updating:

    foreach ($ids_new_arr as $k => $d) {
      if (is_array($d)) {
    	$sub_entry_meta[] = array(
    	  'name' => $k,
    	  'value' => maybe_serialize( array( $d ) )
    	);
      } else {
    	$sub_entry_meta[] = array(
    	  'name' => $k,
    	  'value' => $d
    	);
      }
    }
    
    $entry_meta = array($sub_entry_meta);

    The array it produces looks like:

    Array
    (
      [0] => Array
        (
          [0] => Array
            (
              [name] => text-1
              [value] => testmon
            )
    
          [1] => Array
            (
              [name] => name-1
              [value] => a:1:{i:0;a:2:{s:10:"first-name";s:5:"teddy";s:9:"last-name";s:7:"testmon";}}
            )
        )
    )

    When I run:

    $update_entry = Forminator_API::update_form_entry( $form_id, $do_entry_id, $entry_meta );

    $update_entry returns ‘true’, but the data does not update.

    • This reply was modified 1 year, 2 months ago by jamminjames.
    Thread Starter jamminjames

    (@jamminjames)

    P.S. How we get some kind of info on the result of ‘Forminator_API::update_form_entry’? Can’t find anything in your documentation about viewing result objects.

    The documentation says that upon success, ‘Forminator_API::update_form_entry’ returns the Entry ID that was being edited (not ‘true’ as I’d assumed). However, in my case, it returns “1” every time.

    • This reply was modified 1 year, 2 months ago by jamminjames.
    • This reply was modified 1 year, 2 months ago by jamminjames.
    Thread Starter jamminjames

    (@jamminjames)

    Okay, I finally got it. I had the wrong thing in the maybe_serialize function. I had “maybe_serialize( array($d) )”, instead of simply “maybe_serialize( $d )”. It’s working now. My final PHP code, for anyone interested, is:

    foreach ($field_entries as $k => $d) {
      $serial = maybe_serialize( $d );
      if (is_array($d)) {
        $sub_entry_meta[] = array(
        'name' => $k,
        'value' => $serial
        );
      } else {
        $sub_entry_meta[] = array(
        'name' => $k,
        'value' => $d
        );
      }
    }
    $entry_meta = $sub_entry_meta;
    
    // update entry in db table
    $update_entry = Forminator_API::update_form_entry( $form_id, $do_entry_id, $entry_meta );

    I’m still curious about how to view result objects though, for info on what happened after executing methods like ‘Forminator_API::update_form_entry’.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jamminjames

    Thanks for update!

    I’m sorry for not being able to get back to you earlier but I’m glad you’ve figured it out.

    As for “viewing result objects”, I’m not quite sure if I understand it correctly but let me try ?? :

    The “update_form_entry” method only returns entry ID if update successful so there’s no way to “directly” see anything.

    The entry (as in “submission”) is updated directly in DB so you should see changes reflected in “Forminator -> Submissions” but if you want to check it “by code” you simply need to use another method after the update the “get_entry()” one, passing form ID and that entry ID to it.

    https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-get_entry

    This will fetch and return that entry for you and if you call it after “update_form_entry()” it should return already updated one.

    Best regards,
    Adam

    Thread Starter jamminjames

    (@jamminjames)

    Okay, thanks. But the “update_form_entry” method does NOT seem to return the entry ID if update successful, it’s always “1”, or “0” if not successful. WPMU ought to update that in the documentation.

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @jamminjames,

    Hope this message finds you well and thank you for bringing this to our attention.

    I did share it with our Devs and Docs team.

    Best regards,
    Laura

    Thread Starter jamminjames

    (@jamminjames)

    Well, now I’m not sure. The reason I thought this is that after running

    $update_entry = Forminator_API::update_form_entry( $form_id, $do_entry_id, $entry_meta );

    … I echo the “$update_entry” variable, and it always shows as “1”. However, when I test:

    if ($update_entry==$do_entry_id)

    … it passes as true, and since “$do_entry_id” is the form entry ID, it would seem that “$update_entry” is returning a match for that. Not sure why it echoes “1” though.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jamminjames

    Thanks for response.

    The “update_form_entry()” as it turns out doesn’t return form ID. I was wrong on that (and I have already asked our docs team to update API docs as that’s where I took that information from). It actually returns boolean true/false (that’s why it “echoes” 1 or 0 always) or WP error instead.

    However, you actually do need to read the entry ID since you already have it – you are updating entry instead of adding new one so ID will be the same. Therefore, after update is made you can simply do this

    if ( ! is_wp_error( $update_entry ) ) {
    
    // and here you can e.g. fetch and check the updated entry
    
    }

    and this should do the trick.

    Best regards,
    Adam

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Using method Forminator_API::update_form_entry’ is closed to new replies.