在我的插件中,如果DB中已经存在特定表的一列,我想检查激活情况,以避免在每次激活时实现它。
类似这样:
class MainClassAddon{
public function __construct(){
register_activation_hook( __FILE__, array( $this, \'install\' ) );
}
public function install(){
if( /*Checking if column c in table wp_t exists*/ ){
$wpdb->query("ALTER TABLE wp_t ADD c INT(1) NOT NULL DEFAULT 1");
}
}
}
new MainClassAddon();
register_uninstall_hook( __FILE__, array( \'PluginNamespace\\MainClassAddon\', \'uninstall\' ) );
最合适的回答,由SO网友:J.BizMai 整理而成
我找到了这个解决方案:here
所以
class MainClassAddon{
public function __construct(){
register_activation_hook( __FILE__, array( $this, \'install\' ) );
}
public function install(){
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = \'wp_t\' AND column_name = \'c\'" );
if(empty($row)){
$wpdb->query("ALTER TABLE wp_t ADD c INT(1) NOT NULL DEFAULT 1");
}
}
}
new MainClassAddon();
register_uninstall_hook( __FILE__, array( \'PluginNamespace\\MainClassAddon\', \'uninstall\' ) );