createTable()

^[GS]^

Member
I cant use createTable :/

PHP:
<?php

namespace GSZone\test;


use XF\AddOn\AbstractSetup;
use XF\Db\Schema\Create;

class Setup extends AbstractSetup
{
    public function install(array $stepParams = [])
    {
        $sm = $this->schemaManager();
        $sm->createTable('xf_test', function(Create $table)
        {
            $table->addColumn('test_id', 'int')->autoIncrement();
            $table->addColumn('name', 'varchar', 128);
            $table->addColumn('enabled', 'tinyint', 3)->setDefault(1);

            $table->addPrimaryKey('test_id');
        });
    }

    public function upgrade(array $stepParams = [])
    {
        // TODO: Implement upgrade() method.
    }

    public function uninstall(array $stepParams = [])
    {
        $sm = $this->schemaManager();
        $sm->dropTable('xf_test');
    }
}

What I do wrong?

No errors, no log, no table....
 
@^[GS]^ I'm no php developer and still learning the ropes but this is what I did to create a table and it worked just fine for me. Followed the developer docs.

PHP:
<?php

namespace ThemeHorizon\MostEverOnline;

use XF\Db\Schema\Create;


class Setup extends \XF\AddOn\AbstractSetup
{
    use \XF\AddOn\StepRunnerInstallTrait;
    use \XF\AddOn\StepRunnerUpgradeTrait;
    use \XF\AddOn\StepRunnerUninstallTrait;

    public function installStep1()
    {
        $this->schemaManager()->createTable('xf_mosteveronline', function (Create $table) {
            $table->addColumn('user_id', 'int')->setDefault(0);
            $table->addColumn('guest_id', 'int')->setDefault(0);
        });
    }

    public function uninstallStep1()
    {
        $this->schemaManager()->dropTable('xf_mosteveronline');
    }



}

You'll need to replace the table names of course.

You also need to run the install steps to get it to work. You can't just make the code and expect it to create inside the database. You must run the installs step via the command line as it says in the developer docs (now that @Chris D has updated it).

Code:
php cmd.php xf-addon:install-step [addon_id] [step]

You can view the developer docs example add-on that details this here:

https://xenforo.com/xf2-docs/dev/lets-build-an-add-on/
 
Back
Top Bottom