• Resolved natriya

    (@natriya)


    Hi @xnau
    Following this topic https://ww.wp.xz.cn/support/topic/generate-member-id-when-importing-csv-data/, i was trying to generate an member_id when importing a csv also.
    I did it successfully with this code :

    add_filter( ‘pdb-before_csv_store_record’, array( $this, ‘manage_new_member_id_csv’ ) );

    public function manage_new_member_id_csv($record) {

    global $wpdb;
    $sql = ‘SELECT id FROM ‘ . Participants_Db::$participants_table . ‘ WHERE ‘ . last_name . ‘ = “%s” AND ‘ . first_name . ‘ = “%s” AND ‘ . email . ‘ = “%s”‘;
    $result = $wpdb->get_col( $wpdb->prepare( $sql, $record[“last_name”], $record[“first_name”], $record[“email”] ) );
    if ( $wpdb->num_rows === 0 ) {
    $record = $this->generate_new_member_id($record);
    } else {
    $record[$this->id_field] = null;
    }
    return $record;
    }

    When I import a new csv with the member_id field in the csv file, it works properly. However, when the member_id field is not in the csv file, the member_id is not inserted in the database (even though the member_id is generated successfully )

    While trying to debug, I noticed in the plugin code that, the csv fields which were treated were only those in the csv file :

    class PDb_CSV_Import extends xnau_CSV_Import
    […]
    function store_record( $post ) {
    […]
    // add the record data to the database
    $id = Participants_Db::process_form( $post, ‘insert’, false, $this->column_names );

    Is there a way to force the treatment of a field when importing a csv file even if the field name is not in the csv file ? If it is not possible currently, would it be possible to add a filter in the plugin so that we would be able to add extra fields which are not in the csv file ?

    I would be needing that because I would like to force the member_id to be generated and inserted in database even if the field name is not in the csv file, so that, even if the users who are importing the csv file forget to include this field in the file, the member_id would still be generated

    Thank you
    Have a nice day

    • This topic was modified 8 years, 1 month ago by natriya.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Roland Barker

    (@xnau)

    Not possible to add an item to the CSV data because the set of columns that can be included is determined by the CSV file header and there is currently no way to change that with a filter.

    You can get around this by setting up an action on ‘pdb-after_import_record’ that saves the new ID to the record in a separate operation if there isn’t one in the CSV. Writing a value to a record is easy using the Participants_Db::write_participant($data,$id) method.

    Thread Starter natriya

    (@natriya)

    Thank you for the tip, it works like a charm 🙂

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

The topic ‘force field in csv import’ is closed to new replies.