web-dev-qa-db-ja.com

フィールドがまだ作成されていないときにデータベーステーブルにデータを挿入する方法

ユーザー登録(プラグイン)を処理する既存のテーブル(既にdBで作成されている)があり、$ wpdB-> insertを使用してデータを挿入するとします。

        $success = $wpdb->insert(
            $wpdb->mytable,
            array(
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email' => $email,
        'activation_key' => $activation_key,
            ), array('%s', '%s', '%s', '%s')
    );

今私の問題は私がdBで私の既存のテーブルに別のフィールドを追加したいということです。このフィールドには独自のデータもあります。このような状況に対処するためのベストプラクティスは何でしょうか。 $ wpdB-> insertクラスがdBにない場合は自動的に列を作成しますか?たとえば、挿入に趣味のフィールド名を追加したいとします。

        $success = $wpdb->insert(
            $wpdb->mytable,
            array(
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email' => $email,
        'hobby' => $hobby,            
        'activation_key' => $activation_key,
            ), array('%s', '%s', '%s', '%s')
    );

「趣味」データは新しい列で自動的にdBに挿入されますか?そうでなければ、これを行う際の最も簡単で推奨される方法は何でしょうか。助けてくれてありがとう。

1
Emerson Maningo

いいえ、データを挿入する前に列が存在している必要があります。そうでなければ、照会は失敗します。

新しい列に合わせてテーブル作成SQLクエリを編集する必要があります。その後、もう一度dbDelta()を介して実行します。 dbDelta()はあなたのクエリとテーブル構造を比較し、足りないカラムだけを作成します。

データベース構造が最新であるかどうかを判断する最良の方法は、プラグインのバージョンをオプションに格納し、それを現在のプラグインのバージョンと比較することです。

例:

class Awesome_Plugin {

    /** current plugin version */
    public $version = '4.2';

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( version_compare( $installed_ver, $this->version, '!=' ) ) {

            $sql = "CREATE TABLE {$wpdb->prefix}awesome_plugin (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
                name tinytext NOT NULL,
                text text NOT NULL,
                url VARCHAR(100) DEFAULT '' NOT NULL,
                UNIQUE KEY id (id)
            );";

            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$awesome_plugin = new Awesome_Plugin();

もっと読むことができます WordPressコーデックス

2
shea

もう一つの例:

/**
 * Register new database table
 */
add_action( 'init', 'register_litho_quiz_table', 1 );
add_action( 'switch_blog', 'register_litho_quiz_table' );
function register_litho_quiz_table() {
    global $wpdb;
    $wpdb->litho_quiz_results = "{$wpdb->prefix}quiz_results";
    $wpdb->litho_quiz_questions = "{$wpdb->prefix}quiz_questions";
    $wpdb->litho_quiz_choices = "{$wpdb->prefix}quiz_choices";
}

/**
 * Create new database table
 */
function create_quiz_tables() {
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    global $wpdb;
    /*
     * The global $charset_collate contains the character set and
     * collation used by the native WordPress tables. Loosely, these
     * define the encodings of characters and how they are compared -
     * given that WordPress is used in many different languages it's
     * important to use the correct collation for your table.
     */
    global $charset_collate;
    // Call this manually as we may have missed the init hook
    register_litho_quiz_table();


    /*
     * log_id - the log ID.
     user_id - the user ID for whom the log corresponds.
     activity - the activity that occurred.
     object_id - the ID of the object (e.g. post ID, user ID, comment ID etc) that was the subject of the user's activity.
     object_type - the type of object (e.g. 'post', 'user', 'comment' etc).
     activity_date - the datetime of the activity.

    table
        --key--     --img(255)--     --string(255)--
        resultsid   resultsimg  resultsdescription
        resultsid   resultsimg  resultsdescription
    table
        --key-      --string--
        questionid  question
        questionid  question
        questionid  question
    table
    --key--    --foreign-- --foreign-- --img(255)-- --string(20)--
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
    choiceid    questionid  resultid    choiceimg   choicetitle
     */
    $sql_create_results = "CREATE TABLE {$wpdb->litho_quiz_results} (
        results_id int(2) unsigned NOT NULL auto_increment,
        results_img longblob NOT NULL,
        results_description varchar,
        PRIMARY KEY  (results_id)
    ) $charset_collate; ";
    dbDelta( $sql_create_results );

    $sql_create_questions = "CREATE TABLE {$wpdb->litho_quiz_questions} (
        question_id int(2) unsigned NOT NULL auto_increment,
        question_description varchar,
        PRIMARY KEY  (question_id)
    ) $charset_collate; ";
    dbDelta( $sql_create_questions );

    $sql_create_choices = "CREATE TABLE {$wpdb->litho_quiz_choices} (
        choice_id int(2) unsigned NOT NULL auto_increment,
        choice_img longblob,
        choice_description varchar,
        question_id int(2) NOT NULL,
        results_id int(2) NOT NULL,
        results_description varchar NOT NULL,
        PRIMARY KEY  (choice_id)
        FOREIGN KEY (question_id) REFERENCES items(question_id),
        FOREIGN KEY (results_id) REFERENCES items(results_id)
    ) $charset_collate; ";
    dbDelta( $sql_create_choices );
}
// Create tables on plugin activation
register_activation_hook( __FILE__, 'create_quiz_tables' );
0
davidcondrey