Skip to:
Content
Pages
Categories
Search
Top
Bottom

Q: working with $wpdb class and Buddypress 1.5 (custom SQL calls)


  • sdls
    Member

    @simon_said

    Hey all, I’m trying to find the proper way of referencing BP tables using the $wpdb class. using $wpdb->$table reference http://codex.wordpress.org/Class_Reference/wpdb#Tables allows a smart way of reference WP table in case someone changes the table prefix.

    Hoping to follow suite when referencing the BP tables, for example $wpdb->$xprofile (or something like that). Can’t find these BP table references anywhere… my goal is to add a record to wp_bp_xprofile_data correctly following the new BP 1.5 etiquette.

    Any suggestions on how to keep this bulletproof would be greatly appreciated. THANKS!

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

  • sdls
    Member

    @simon_said

    Something like this

    `
    $sql = “SELECT *
    FROM wp_bp_xprofile_data XP
    WHERE XP.user_id = $iallusersUserID”;
    $xprofile = $wpdb->get_col( $wpdb->prepare($sql));
    `

    should probably more like this

    `
    $sql = “SELECT *
    FROM $wpdb->x_profile XP
    WHERE XP.user_id = $iallusersUserID”;
    $xprofile = $wpdb->get_col( $wpdb->prepare($sql));
    `

    Maybe this whole style of calling a custom SQL query is way of track? any suggestions?


    Boone Gorges
    Keymaster

    @boonebgorges

    Components register their table names themselves. See https://buddypress.trac.wordpress.org/browser/trunk/bp-xprofile/bp-xprofile-loader.php#L85

    In the case of xprofile, they are
    `$bp->profile->table_name_data
    $bp->profile->table_name_fields`
    and so on.

    You may find it helpful to look at https://buddypress.trac.wordpress.org/browser/trunk/bp-xprofile/bp-xprofile-classes.php to see how BP itself references these tables. Likewise for the other components.


    Boris
    Participant

    @travel-junkie

    If you want to add a record, then there are API functions to do that for you for the most part. `xprofile_insert_field()` is one such function. There’s more in bp-xprofile/bp-xprofile-functions.php.

    If you really do need to query the database directly, then you can find the table names within the $bp global, e.g. `$bp->groups->table_name` for the main groups table.

    If there are API functions, then you should use these. Only query directly if you can’t find a function to do the heavy lifting for you.


    Boone Gorges
    Keymaster

    @boonebgorges

    +1 to using the `xprofile_` functions if you can.


    sdls
    Member

    @simon_said

    Thanks both for the feedback!… looking into xprofile_insert_field() in bp-xprofile/bp-xprofile-functions.php. This appears to be the function for adding a new profile field to the BP system.

    `function xprofile_insert_field( $args = ” ) {
    global $bp;

    extract( $args );

    /**
    * Possible parameters (pass as assoc array):
    * ‘field_id’
    * ‘field_group_id’
    * ‘parent_id’
    * ‘type’
    * ‘name’
    * ‘description’
    * ‘is_required’
    * ‘can_delete’
    * ‘field_order’
    * ‘order_by’
    * ‘is_default_option’
    * ‘option_order’
    */

    // Check we have the minimum details
    if ( !$field_group_id )
    return false;

    // Check this is a valid field type
    if ( !in_array( $type, (array) $bp->profile->field_types ) )
    return false;

    // Instantiate a new field object
    if ( $field_id )
    $field = new BP_XProfile_Field( $field_id );
    else
    $field = new BP_XProfile_Field;

    $field->group_id = $field_group_id;

    if ( !empty( $parent_id ) )
    $field->parent_id = $parent_id;

    if ( !empty( $type ) )
    $field->type = $type;

    if ( !empty( $name ) )
    $field->name = $name;

    if ( !empty( $description ) )
    $field->description = $description;

    if ( !empty( $is_required ) )
    $field->is_required = $is_required;

    if ( !empty( $can_delete ) )
    $field->can_delete = $can_delete;

    if ( !empty( $field_order ) )
    $field->field_order = $field_order;

    if ( !empty( $order_by ) )
    $field->order_by = $order_by;

    if ( !empty( $is_default_option ) )
    $field->is_default_option = $is_default_option;

    if ( !empty( $option_order ) )
    $field->option_order = $option_order;

    return $field->save();
    }
    `
    All I really need to do is add a little record into the existing “wp_bp_xprofile_data” table

    This below code works however it’s definately not Open source friendly or BP friendly

    `

    // grab this users nicename

    $user = get_userdata( $iallusersUserID );
    $nicename = $user->user_nicename;
    $last_udpated = date(‘Y-m-d H:i:s’);

    // create a new record in bp_xprofile_data and add the nicename

    $wpdb->insert(
    ‘wp_bp_xprofile_data’,
    array(
    ‘field_id’ => 1,
    ‘user_id’ => $iallusersUserID,
    ‘value’ => $nicename,
    ‘last_updated’ => $last_udpated
    ),
    array(
    ‘%d’,
    ‘%d’,
    ‘%s’,
    ‘%s’
    )
    );
    `

    Any thoughts? Thanks again!


    Boone Gorges
    Keymaster

    @boonebgorges

    That’s what `xprofile_set_field_data()` is for.

    `xprofile_set_field_data( 1, $iallusersUserID, $nicename );`

    No need to use $wpdb!


    sdls
    Member

    @simon_said

    thanks @boonebgorges !

    WOW searching through all these new functions will be time consuming at first, but I’m sure will make things better over the long term. are these new to BP 1.5?

    I’m so used to just using SQL to solve my problems these seems way complicated… (above my head and slightly to the left) So let’s say I just want to check for the whether a record exists, is it suggested that these BP functions should be used for all database communication?

    for example… this checks for the existence of the field in the first place

    `
    $sql4 = “SELECT *
    FROM wp_bp_xprofile_data XP
    WHERE XP.user_id = $iUserID”;
    $acheck_for_user_xprofile = $wpdb->get_col( $wpdb->prepare($sql4));
    if ($acheck_for_user_xprofile) {
    $check_for_user_xprofile_answer = “yes“;
    } else {
    $check_for_user_xprofile_answer = “no”;
    }
    `


    Boone Gorges
    Keymaster

    @boonebgorges

    The functions are not new – the whole point of a framework like BuddyPress, from the beginning, has been so that you *don’t* have to reinvent the wheel :)

    I think that for your purposes you can use `xprofile_get_field_data()`. See bp-xprofile/bp-xprofile-functions.php.


    sdls
    Member

    @simon_said

    understood.. thanks again Boone!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Q: working with $wpdb class and Buddypress 1.5 (custom SQL calls)’ is closed to new replies.
Skip to toolbar