如何在wordpress中正确创建删除表?

时间:2016-07-15 09:19:03

标签: php wordpress plugins

我正在开发一个wordpress插件,但我有一个问题: 当我激活插件时,我收到错误消息" 该插件生成3个意外输出字符 "

我的目的是创建一个插件,在激活时创建一些表,并在unactivate时销毁它们。

这是我的主要文件代码:

<?php
/*
Plugin Name: _Innovation Factory
Description: Aportaci&oacute;n y mantenimiento de ideas del staff para mejorar el hotel.    
Version: 0.1    
Author: Extra Software - Thankium
*/

require_once('includes/php/constants.php');

//creacion de las tablas necesarios
function innovationFactoryActivacion() {
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_FASES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));"; 

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_ESTADOS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_HOTELES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_AREAS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `anonima` TINYINT NOT NULL,
        `fecha_alta` TIMESTAMP NULL DEFAULT NOW(),
        `nombre` VARCHAR(250) NULL,
        `observacion` TEXT NULL,
        `desarrollo` TEXT NOT NULL,
        `id_hotel` BIGINT(20) NULL,
        `id_estado` BIGINT(20) NOT NULL,
        `id_fase` BIGINT(20) NOT NULL,
        `id_area` BIGINT(20) NOT NULL,
        `id_usuario` BIGINT(20) NOT NULL,
        PRIMARY KEY (`id`));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_ADJUNTOS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(1000) NOT NULL,
        `fecha_alta` TIMESTAMP NOT NULL DEFAULT now(),
        `id_idea` BIGINT(20) UNSIGNED NOT NULL,
        PRIMARY KEY (`id`),
        INDEX `ID_IDEA_idx` (`id_idea` ASC),
        CONSTRAINT `ID_IDEA`
        FOREIGN KEY (`id_idea`)
        REFERENCES `" . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . "` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_VOTACIONES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `id_usuario` BIGINT(20) UNSIGNED NOT NULL,
        `id_idea` BIGINT(20) UNSIGNED NOT NULL,
        `valoracion` INT NOT NULL,
        `fecha_valoracion` TIMESTAMP NOT NULL DEFAULT NOW(),
        PRIMARY KEY (`id`),
        INDEX `ID_IDEA_idx` (`id_idea` ASC),
        INDEX `ID_USUARIO_idx` (`id_usuario` ASC),
        CONSTRAINT `ID_IDEA`
        FOREIGN KEY (`id_idea`)
        REFERENCES `" . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . "` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
        CONSTRAINT `ID_USUARIO`
        FOREIGN KEY (`id_usuario`)
        REFERENCES `".$wpdb->prefix."users` (`ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);";

    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_FASES_ . "(`nombre`) VALUES ('Buzón'), ('Análisis'), ('Piloto'), ('Standard');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_ESTADOS_ . "(`nombre`) VALUES ('Recibida'), ('Aprobada'), ('Rechazada'), ('Publicada'), ('En proceso'), ('Finalizada');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_AREAS_ . "(`nombre`) VALUES ('Producto'), ('Servicio'), ('Cliente');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_HOTELES_ . "(`nombre`) VALUES ('Petit Palace Alcalá'), ('Icon Retiro by Petit Palace'), ('Petit Palace Arana'), ('Petit Palace Ópera'), ('Petit Palace Art Gallery'), ('Petit Palace Arturo Soria'), ('Petit Palace Barcelona'), ('Old: Boquería y Opera Garden'), ('Petit Palace Plaza de la Reina'), ('Petit Palace Sevilla'), ('Petit Palace Callao'), ('Petit Palace Chueca'), ('Petit Palace Serrano'), ('Petit Palace Ruzafa'), ('Petit Palace El Prado'), ('Petit Palace Preciados'), ('Petit Palace Madrid Aeropuerto'), ('Petit Palace Catedral'), ('Petit Palace Plaza Mayor'), ('Petit Palace Paseo de Gracia'), ('Petit Palace Plaza Larios'), ('Petit Palace Plaza del Carmen'), ('Petit Palace Posada del Peine'), ('Petit Palace Castellana'), ('Petit Palace Puerta del Sol'), ('Petit Palace Plaza España'), ('Petit Palace Santa Bárbara Plaza'), ('Petit Palace Santa Cruz'), ('Petit Palace Tamarises'), ('Petit Palace Gran Vía'), ('Petit Palace Triball');";

    foreach($sql as $query){
        dbDelta($query);
    }

}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__, 'innovationFactoryActivacion');

register_deactivation_hook(__FILE__, function(){
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $sql = array();
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_VOTACIONES_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_ADJUNTOS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_IDEAS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_AREAS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_HOTELES_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_ESTADOS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_FASES_.";";

    foreach($sql as $query){
        $wpdb->query($query);
    }
});

表格是正确创建和删除的,但我不知道我有这个输出。

在某些表中,它可能与我的FK约束有关系吗?

谢谢你...

2 个答案:

答案 0 :(得分:1)

您的WordPress数据库表格有Foreign Key Constrains。这意味着一个表中的一个字段被引用为其他表的字段之一。所以基本上,当你丢弃一个表时,如果你有这个外键约束,那些字段可能不会被删除。

您可以在下面阅读一些详细信息。

https://gauravsohoni.wordpress.com/2009/03/09/mysql-disable-foreign-key-checks-or-constraints/

Can a foreign key be NULL and/or duplicate?

答案 1 :(得分:0)

$wpdb正在制造问题。在你的函数中声明global $wpdb;