Skip to:
Content
Pages
Categories
Search
Top
Bottom

buddypress-skeleton-component – saving to database?


  • thekmen
    Participant

    @thekmen

    Hi all,
    I’m playing with the buddypress-skeleton-component, but can’t get it to save data to a new database table.
    something like:

    function add_my_data( $field_1, $field_2, $field_3 ) {
    global $bp;
    $update = new BP_Example_Template();
    $update->field_1 = $field_1;
    $update->field_2 = $field_2;
    $update->field_3 = $field_3;

    $save = $update->save();
    return $save;
    }

    any ideas what I am doing wrong?

Viewing 16 replies - 1 through 16 (of 16 total)

  • Jeff Sayre
    Participant

    @jeffsayre

    Do you have any errors in the log files?

    I assume that you are passing data into your function of the proper type. If so, then the next issue is to determine if the save method is receiving the data and if it’s provided in the way in which it expects. The example save method in the Skeleton Component is coded to accept all integer data and no string data. This is why I asked if you are passing the proper data type to the method for each field.

    Your log files and some custom debugging strings should help figure this out pretty easily.


    thekmen
    Participant

    @thekmen

    Thanks @jeffsayre,
    No errors & the save method seems to be receiving the data.
    using:

    function add_my_data( $field_1, $field_2, $field_3 ) {
    global $bp;
    $update = new BP_Example_TableName();
    $update->field_1 = $field_1;
    $update->field_2 = $field_2;
    $update->field_3 = $field_3;

    $save = $update->save();
    return $save;
    }

    function data_to_add() {
    add_my_data('100', '200','6');
    }

    if I add

    add_action('wp','data_to_add');

    the data is inserted on page load but if I use:

    add_action('bp_activity_posted_update','data_to_add');

    nothing gets added to the database.
    Any ideas?


    Jeff Sayre
    Participant

    @jeffsayre

    It sounds like you may have a hook sequence firing issue. In other words, your added action function might not be getting fired. This could be an issue with your code or even the BuddyPress code.

    You can download my plugin WordPress Hook Sniffer to see if that might be the case. It takes awhile to figure out how to use my plugin but the two articles I wrote should guide you through the process.


    thekmen
    Participant

    @thekmen

    Thanks @jeffsayre that’s some serious reading…
    With ‘Action Event Firing Order’ turned on, I don’t see data_to_add being fired.
    With ‘Action Event Firing’ turned on, I do get ‘Firing Sequence 71: bp_activity_posted_update –> data_to_add –> Time fired: 1273766497.1222’ after posting an update.

    I’m still completely lost though…


    Jeff Sayre
    Participant

    @jeffsayre

    The “Action Event Firing Order” is an array that holds all action hooks that will be fired. Since your function data_to_add does not have a hook–i.e. it does not have a do_action( ‘data_to_add’); call–there will be nothing in that array. So, that is fine.

    But, it does seem that the action event bp_activity_posted_update is firing your hooked to action function. The next step is to make sure that you are attaching to, hooking into, the proper action event hook. The hook bp_activity_posted_update passes three parameters, so hooking your custom function to it is not the best idea, unless you are trying to modify any of those three parameters. What exactly are you trying to accomplish?


    thekmen
    Participant

    @thekmen

    No, I’m not trying to modify any of those three parameters. I just want to add separate data to a new DB table when a user posts an update.
    Thanks again for all your help with this…


    Jeff Sayre
    Participant

    @jeffsayre

    Are you sure the data is not being added? It appears that your function is firing. Do you mean to say instead that the saved data does not get displayed after the function has fired?

    There is nothing in the few lines of code you’ve pasted above that would make your data_to_add() work when hooked to the wp action event but not the bp_activity_posted_update action event. Since both events get fired and subsequently fire your added action function, the data should be written to the DB in either case.


    thekmen
    Participant

    @thekmen

    yeah, i’m sure, watching the table in phpMyAdmin.
    Can’t figure it out either, it’s just the bare bones skeleton component using the BP_Example_TableName class & does work when using add_action('wp','data_to_add'); so driving me nuts…


    thekmen
    Participant

    @thekmen

    Just tried
    add_action('groups_group_create_complete','data_to_add');
    and it works! Is there something other than bp_activity_posted_update I should be using for updates maybe?


    Tosh
    Participant

    @xberserker

    @thekmen The other is:
    bp_groups_posted_update

    What are you working on?


    thekmen
    Participant

    @thekmen

    @xberserker cool, will try that.
    Sending you over version of your plugin with Points page, let me know if you need any help with it.


    Tosh
    Participant

    @xberserker

    Looks good so far! :) I’ll fully test it tonight.


    thekmen
    Participant

    @thekmen

    @xberserker the version I sent you over doesn’t save the points to the DB, it still relies on CubePoints for this.
    I just left the save functions in so we can use them at a later date if we want.


    thekmen
    Participant

    @thekmen

    Sorry, only getting back to this now.
    @jeffsayre or anyone else who might know,
    I realised the saving to DB only works on the non ajax updates, like creating a new group.
    If I turn off JS in my browser, the saving to DB does work on previous ajax updates, like updating your status.

    Any idea how I would get it to save to DB when using the default BP ajax updates?


    thekmen
    Participant

    @thekmen

    ok, to update a database table on ajax updates, I think I need to add the action to ‘wp’, something like:

    function data_to_add() {
    global $bp;

    if ( !is_user_logged_in() || $bp->current_component != $bp->activity->slug || $bp->current_action != 'post')
    return false;

    check_admin_referer( 'post_update', '_wpnonce_post_update' );

    add_my_data('100', '200','6');
    }

    add_action('wp','data_to_add', 3);

    however, it still doesn’t work. any ideas?
    @DJPaul maybe you would know? how are you doing it with your new version of the achievments plugin?

    Have a look at or refer to https://codex.wordpress.org/AJAX_in_Plugins. I’d also suggest you do a find-in-files in BuddyPress for “wp_ajax_” to see how BuddyPress does it.

Viewing 16 replies - 1 through 16 (of 16 total)
  • The topic ‘buddypress-skeleton-component – saving to database?’ is closed to new replies.
Skip to toolbar