Recent Topics

1 Jul 14, 2006 09:24    

I'm trying to make a shiny new plugin and am, of course, completely stumped at every turn. Very simply, a plugin to automagically link words and phrases to URLs. To do this I decided I would have my plugin create a table that the user could add to or subtract from via their tools::new tab. I also want to drop the table when the user un-installs the plugin, and that's what I'm looking for help with today.

	function BeforeUninstall()
		$plugin_created_table = $tableprefix.'plugin_created_table';
		$dropSQL = "DROP TABLE $plugin_created_table";
		return true;

That's not going to work because I never use $dropSQL to actually drop the table, thus my question: How, inside a plugin in b2evolution, can I drop a table the plugin created? Ideally it would be in the format that returns a true if it actually drops.

	function BeforeUninstall()
		$plugin_created_table = $tableprefix.'plugin_created_table';
		$dropSQL = "DROP TABLE $plugin_created_table";
		return $SomethingGroovy( $dropSQL );

Normally all I do is find a part of b2evolution that does what I want then tweak it to do what I want. In this case I can't think of a single instance where b2evolution drops a table, so I'm sort of stuck. Plus I admit I'm totally lost in the "new" way b2evo does it's thing. :'(



Also is there a preferred naming convention for tables that a plugin creates? In the code snippets above I just faked it, but I figured for sure I should use the blog owner's $tableprefix value.

2 Jul 14, 2006 09:44

It looks like a plugins tables are dropped automatically when you uninstall which saves a load of messing around huh?

For the naming convention, I believe this is how it's meant to go (I use a function to set the names once so I don't have to type all the $this->get_sql_table() everytime ;) ):-

// sets the database table names for the plugin
	function get_tables()
		if( !isset( $this->tags_table ) ) $this->tags_table = $this->get_sql_table( $this->tagsTable );
		if( !isset( $this->posts_table ) ) $this->posts_table = $this->get_sql_table( $this->postsTable );

If you want a copy of the plugin this code came from to roam around in then me know (it's nowhere near finished but it uses the tools tabs and forms etc, so you might find it useful ...... and if not there's always the recycle bin :p)


3 Jul 14, 2006 09:58

The table I created via plugin lasted after uninstalling and deleting the plugin, so I figure I gotta drop it somehow. I used the BeforeInstall() function to build it, and checked via phpmyadmin to see that I was actually successful. When I un-installed it was still there, so I figured I had to use BeforeUninstall() to make it go away.

Yeah: a peek at something smart-based would be cool. I'm slowly stumbling through this, and want to make it as 'b2evoish' as possible, but damn I'm lost.

4 Jul 14, 2006 10:07

I wonder if it's because of the way I created my tables? Or possibly something to do with the naming convention? Funny enough, I actually had a "drop tables" routine in there, but I removed it to see what would happen and they we're automatically dropped for me.

I'll whizz a copy across to you so you can tinker with it ;)


5 Jul 14, 2006 17:27

Hooray for random guessing!

I found an answer to my dilemma without re-writing what little bit I had so far. For those interested I'll show what I learned. First I create a table with what I considered a reasonable naming convention:

	function BeforeInstall() {
		global $Plugins, $DB, $tableprefix;
		$plugin_pluginname_table = $tableprefix.'plugin_pluginname';
		$query = "CREATE TABLE $plugin_pluginname_table (
			pluginname_ID bigint(11) NOT NULL auto_increment,
			pluginname_this varchar(80) NOT NULL,
			pluginname_that varchar(240) NOT NULL,
			pluginname_theotherthing bigint(11) NOT NULL,
			PRIMARY KEY pluginname_ID (pluginname_ID),
			UNIQUE pluginname_text (pluginname_text)
		$DB->query( $query );
		$query = "INSERT INTO $plugin_pluginname_table(pluginname_this, pluginname_that, pluginname_theotherthing) VALUES ('umma', 'gumma', 1)";
		$DB->query( $query );
		$query = "INSERT INTO $plugin_pluginname_table(pluginname_this, pluginname_that, pluginname_theotherthing) VALUES ('foo', 'bar', 9)";
		$DB->query( $query );
		$query = "INSERT INTO $plugin_pluginname_table(pluginname_this, pluginname_that, pluginname_theotherthing) VALUES ('word', 'press', 3)";
		$DB->query( $query );
		return true;

Hooray for plugins that make tables!

The point of this thread was dropping the table on uninstall. Here's how I finally got that to happen, and in retrospect it doesn't look all that complicated:

	function BeforeUninstall()
		global $Plugins, $DB, $tableprefix;
		$plugin_pluginname_table = $tableprefix.'plugin_pluginname';
		$query = "DROP TABLE $plugin_pluginname_table";
		$DB->query( $query );
		return true;

Next up for me is to read the table and spill it's guts onto the tools tab::pluginname subtab. I'm sure it's easy IF you know how ;)

6 Jul 14, 2006 20:30


Are you using this plugin event to create your table? If you do the it will automatically delete your table when you uninstall. I think it may also allow you to do upgrades to a plugin and change the db layout without losing data. Don't quote me on that, though. I got this code from the Akismet plugin.

         * Make a table!
         * @return array
        function GetDbLayout()
                return array(
                                "CREATE TABLE ".$this->get_sql_table('your_table_lable')." (
                                        sb_ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                                        sb_ip varchar( 40 )  NULL,
                                        sb_url varchar( 80 ),
                                        sb_content text,
                                        sb_test text

7 Jul 16, 2006 17:23

Sorry for being late. Here are the facts:
- GetDbLayout() should be used for DB tables. They get created automagically and if the plugin version changes, get looked after (if the DB schema needs to be adopted).
- On uninstall of a plugin, all tables that start with what "Plugin::get_sql_table()" returns, get (asked for being) dropped.

EdB, that's why your table does not get handled automagically: you just prefix it with $tableprefix, but you should use the get_sql_table() method instead.

9 Jul 16, 2006 17:34

Damn, we live and learn :D


10 Jul 16, 2006 19:02

Thanks! It was obvious to me that I was doing *something* completely wrong and would have to start over. Now I know where to start when it's time to start.

I should just sit on the sidelines for a while until lots of people make lots of plugins I can take apart and turn into something I want them to be, but if I do that personman will end up crushing me in the "I post more than you do" contest :lol:

11 Jul 16, 2006 20:12

I'm going to crush you anyway. ;) I've got a handful of plugins done, I just need to clean them up and release them (and deal with all the bugs and feature requests as they come in). Plugins are way more fun and easier to share than hacks, though.

12 Jul 16, 2006 20:40

personman wrote:

I'm going to crush you anyway. ;)

Hmmm.... need a new Strategic Plan... Every time personman posts I reply with "great post personman!" and something to indicate that my post is topical. Let's see how it works out...

Great post personman! However I noticed that gravity fluctuated early this morning due to a dissonant resonance in solar flare activity. Perhaps that is part of the issue?


Plugins are cool. Much better than having to hack core files to make new bells ring, but, as I said, all I do is hack. Therefore for me to do plugins means having other plugins to look at and take apart and rebuild into whatever suits my fancy. Show me *any* hack I did and I'll show you the pre-existing bits that I pulled into a hack. Or I have to try REALLY hard to learn this new fangled stuff :(

13 Jul 16, 2006 21:03

I've got a long way to go to catch up. But if I reply to every one of your replies, then I can at least keep up.

14 Jul 16, 2006 22:07

the perpetual machine that is personperson wrote:

I've got a long way to go to catch up. But if I reply to every one of your replies, then I can at least keep up.

ffs ..... here was me (slowly) gaining on the bloody lemmings post count (I even made it as far as "guru"), but you had to go and let him (that'd be the Super Garralous User Requires Uninstalling) know how I did it ........

I might never forgive yah :|


Form is loading...