MyBB Community Forums

Full Version: $db->createTable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Morning all,

Just curious as to why there is no $db->createTable()? I was about to create a table but am unsure of how to do this properly.

The way I intend to do this is
$this->db->query("CREATE TABLE `mybb_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

However I am unsure if that is correct? Is it safe to assume the charset of UTF8? and Engine of MyISAM?

Cheers
This is the preferred method as far as I know:

	if (!$db->table_exists('post_likes')) {
		$collation = $db->build_create_table_collation();
		$db->write_query("
			CREATE TABLE ".TABLE_PREFIX."post_likes(
				id INT(10) NOT NULL AUTO_INCREMENT,
				post_id INT(10) unsigned NOT NULL,
				user_id INT(10) unsigned NOT NULL,
				PRIMARY KEY (id)
			) ENGINE=MyISAM{$collation};"
		);
	}

You can change to InnoDB if you want.
Ah thanks Euan.

Also does the plugin system not allow OO for calling info, activate, install ect?
Code should be like this:

$plugins->add_hook('postbit', 'MyRPG_postbit');
function MyRPG_postbit(&$post)
{
    ....
}

Notice it's "add_hook" not "run_hooks".

You can't use OO as such for info/activate etc, but you could put the function bodies into a class and call it as an object within those functions. There's not a great deal of point though. The plugin system actively looks for those functions as functions.
Ah yeh stupid mistake, just edited my post realizing it (facepalm).

I tried to do it, however the only way I could get it to work would be to create the object within the function otherwise it would return null.

I have a base class to keep everything clean however the object just returns null when using global.

For example

function MyRPG_postbit(&$post)
{
    global $base;
    var_dump($base);
    $base->postbit(&$post);
}

/**
 * Run all the hooks
 */

$plugins->add_hook("postbit", "MyRPG_postbit");

The var dump returns null even though it is initialized earlier as
$array = 
    array(
        'mybb'      => $GLOBALS['mybb'], 
        'plugins'   => $GLOBALS['plugins'], 
        'templates' => $GLOBALS['templates'], 
        'db'        => $GLOBALS['db']
    );

$base = new Base($array);

Just figured something out though I can't find any connection in the core files I took advice form here (http://community.mybb.com/thread-124593.html) and read that plugin and it seems that this works

$GLOBALS['plugins']->objects['myrpg'] = new Base($array);
function MyRPG_postbit(&$post)
{
    $GLOBALS['plugins']->objects['myrpg']->postbit(&$post);
}

Can anyone explain how this works?
$GLOBALS is a variable defined within PHP itself and stores all global variables. It's therefore accessible anywhere. Plugins is defined as a global variable and as such becomes a key within the $GLOBALS array. I'd assume objects is a property of the Plugin class but I've not looked. If it work, it's all good Toungue
It seems a really long way of doing things when the following should work.

public class foo
{
public function bar()
{
echo 'foobar';
}
}
$foobar = new foo();
function callFooBar()
{
global $foobar;
$foobar->bar();
}
I don't disagree at all.
Does anyone else have any feedback on this or any other alternate ways of doing this?

Thanks Euan Smile
By the way, if you just want to use the object in public areas, you can define it in global_start and make it global. That's what I do with the Alerts object in MyAlerts.
Pages: 1 2