XF 2.0 Setup.php for Addon

LPH

Well-known member
A basic addon was written and is being expanded to use the XF database.

Setup.php
PHP:
<?php

namespace TRN\XenWriter;

use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Db\Schema\Alter;
use XF\Db\Schema\Create;

class Setup extends AbstractSetup
{
   use StepRunnerInstallTrait;
   use StepRunnerUpgradeTrait;
   use StepRunnerUninstallTrait;

   public function installStep1()
   {
      $schemaManager = $this->schemaManager();
      $schemaManager->createTable('xf_xenwriter_document', function (Create $table) {
         $table->addColumn('xenwriter_document_id', 'int')->autoIncrement();
         $table->addColumn('xenwriter_category_id', 'int')->autoIncrement();
         $table->addColumn('parent_category_id', 'int')->setDefault(0);

         $table->addColumn('title', 'varchar', 100)->setDefault('');
         $table->addColumn('description', 'varchar', 255)->setDefault('');
         $table->addColumn('content', 'text');

         $table->addColumn('tag_line', 'varchar', 100)->setDefault('');
         $table->addColumn('date', 'int');
         $table->addColumn('last_update', 'int');

         $table->addColumn('user_id', 'int');
         $table->addColumn('username', 'varchar', 100)->setDefault('');

         $table->addColumn('xenwriter_document_state', 'enum')->values(['visible','moderated','deleted'])->setDefault('visible');

         $table->addColumn('prefix_cache', 'mediumblob');
         $table->addColumn('require_prefix', 'tinyint')->setDefault(0);
         $table->addColumn('thread_prefix_id', 'int')->setDefault(0);
         $table->addColumn('thread_node_id', 'int')->setDefault(0);
         $table->addColumn('discussion_thread_id', 'int')->comment('Points to an automatically-created thread for this document');

         $table->addColumn('rating_count', 'int')->setDefault(0);
         $table->addColumn('rating_sum', 'int')->setDefault(0);
         $table->addColumn('rating_avg', 'float', '')->setDefault(0);
         $table->addColumn('rating_weighted', 'float', '')->setDefault(0);

         $table->addPrimaryKey('xenwriter_document_id');

         $table->addKey(['xenwriter_category_id', 'last_update'], 'xw_category_last_update');
         $table->addKey(['xenwriter_category_id', 'rating_weighted'], 'xw_category_rating_weighted');
         $table->addKey('last_update');
         $table->addKey('rating_weighted');
         $table->addKey(['user_id', 'last_update']);
         $table->addKey('thread_prefix_id');
         $table->addKey('discussion_thread_id');

      });

      $tables['xf_xenwriter_document_feature'] = function(Create $table)
      {
         $table->addColumn('xenwriter_document_id', 'int');
         $table->addColumn('feature_date', 'int');
         $table->addPrimaryKey('xenwriter_document_id');
         $table->addKey('feature_date');
      };
   }
}
At this point, the addon is in the development environment with a route, etc. This new Setup.php file has been written and saved. How do I get this file to run on the development environment and not have things blow up? I don't want to bugger it up since the addon was written without the database first.

Do I go into AdminCP >> Addons and "sync changes" or rebuild?
 

TickTackk

Formerly batpool52!
In command prompt or whatever you use to run the cli commands
Code:
php cmd.php xf-addon:install-step TRN\XenWriter 1
 

LPH

Well-known member
Thank you. I haven't tried it yet. It seemed better to look over the table creation code and make sure these are appropriate tables and columns to add.

Is 'content' set to 'text' appropriate?
Is 'title' okay to have varchar? I've seen a few addons use text, here.
 

S Thomas

Well-known member
It should match the entity. text / varchar is ok, if your entity matches the limits of your chosen type.
 

LPH

Well-known member
It should match the entity. text / varchar is ok, if your entity matches the limits of your chosen type.
Good point. I was starting with the database and haven't built any entities.

This was the approach
(1) Create the controller and get it routed properly.
(2) Build simple templates
(3) Build simple widgets for placeholders.
(4) Build the database tables

I'm on step 4 and haven't decided on step 5, etc.
 

S Thomas

Well-known member
Yea, when creating the model, I usually create data first aswell so I can play around with that. But I do that directly in the DB so I can immediately start with creating the logic part of the model. Downside obviously, I need a checklist so I never forget something to add to the installer.

So considering your question, XF has a varchar(150) for thread titles and mediumtext for post contents.
 

LPH

Well-known member
Dang it.

Laynes-MBP:community layneheiny$ php cmd.php xf-addon:install-step TRN/XenWriter 1

Running Setup class method installStep1()...

[XF\Db\InvalidQueryException]

MySQL query error [1075]: Incorrect table definition; there can be only one auto column and it must be defined as a key

xf-addon:install-step <id> <step>
Apparently, one of my auto and key definitions is wrong. :p

Update: It's obvious once I went back and looked. The second line for xenwriter_document_id. Sigh.
 
Top