finnlesueurgmailcom - 2 months ago

Hey all!

I'm reading up on making Modules and trying it out for myself. I have a question about how to add new fields using the migration (or not, if there's a more proper way).

I started by doing:

Finns-Air:pyro.test finnlesueur$ php artisan make:addon finnito.module.events
Addon [finnito.module.events] created.
Created Migration: 2018_05_09_193713_finnito.module.events__create_events_fields

Then I created the events stream for the module:

Finns-Air:pyro.test finnlesueur$ php artisan make:stream events finnito.module.events
Created Migration: 2018_05_09_193804_finnito.module.events__create_events_stream

Then I migrated the modules:

Finns-Air:pyro.test finnlesueur$ php artisan migrate --all-addons
Migrating: 2018_05_09_193713_finnito.module.events__create_events_fields
Migrated:  2018_05_09_193713_finnito.module.events__create_events_fields
Migrating: 2018_05_09_193804_finnito.module.events__create_events_stream
Migrated:  2018_05_09_193804_finnito.module.events__create_events_stream

And everything seems to be going well, but I'm pretty stumped on how I should be adding new fields to the table. Out of the box the table has this create statement:

CREATE TABLE `pyrotest_events_events` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `sort_order` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `created_by_id` int(11) DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `updated_by_id` int(11) DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `cb70ab375662576bd1ac5aaf16b3fca4` (`slug`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

And I tried to add a field to my EventFormBuilder.php file:

/**
     * The form fields.
     *
     * @var array|string
     */
    protected $fields = [
        "event_description" => [
            "type" => "anomaly.field_type.textarea",
        ],
    ];

And then to create a new migration (excuse the terrible migration name, hah)

Finns-Air:pyro.test finnlesueur$ php artisan make:migration fieldss --addon=events

Except the created migration just looks like this

<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class FinnitoModuleEventsFieldsss extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // $this->fields()->create([]);
        // $this->streams()->create([]);
        // $this->assignments()->create([]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

And when I migrate it seems to work:

Finns-Air:pyro.test finnlesueur$ php artisan migrate --all-addons
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.
Migrating: 2018_05_09_212753_finnito.module.events__fieldsss
Migrated:  2018_05_09_212753_finnito.module.events__fieldsss
Nothing to migrate.
Nothing to migrate.

and a new entry is created in the table pryotest_streams_fields:

'214','events','event_description','','a:0:{}','1'

but the pryotest_events_events table has not been modified, and if I try add a new entry to the stream I get the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'event_description' in 'field list' (SQL: insert into `pyrotest_events_events` (`event_description`, `updated_by_id`, `created_by_id`, `sort_order`, `slug`, `updated_at`, `created_at`) values (Test!, 1, 1, 1, , 2018-05-09 21:30:51, 2018-05-09 21:30:51))

Any pointers on how I should be adding new fields would be really great! Thanks, Finn :)

Answer

kiltedup - 2 months ago

Hi,

You should add the field to 2018_05_09_193713_finnito.module.events__create_events_fields

/**
 * The form fields.
 *
 * @var array
 */
protected $fields = [
    "event_description" => [
        "type" => "anomaly.field_type.textarea",
    ],
];

And then assign the field in 2018_05_09_193804_finnito.module.events__create_events_stream

/**
 * The stream definition.
 *
 * @var array
 */
protected $stream = [
    'slug'         => 'events',
    'translatable' => true,
];

/**
 * The stream assignments.
 *
 * @var array
 */
protected $assignments = [
    'event_description' => [
        'translatable' => true,
    ],
];

You should then be able to use php artisan migrate --addon=finnito.module.events and the event_description will be added to the table.

Drop the translatable options if not needed.

Hope that works !!!!

Dave

kiltedup - 2 months ago

Hi,

You should add the field to 2018_05_09_193713_finnito.module.events__create_events_fields

/**
 * The form fields.
 *
 * @var array
 */
protected $fields = [
    "event_description" => [
        "type" => "anomaly.field_type.textarea",
    ],
];

And then assign the field in 2018_05_09_193804_finnito.module.events__create_events_stream

/**
 * The stream definition.
 *
 * @var array
 */
protected $stream = [
    'slug'         => 'events',
    'translatable' => true,
];

/**
 * The stream assignments.
 *
 * @var array
 */
protected $assignments = [
    'event_description' => [
        'translatable' => true,
    ],
];

You should then be able to use php artisan migrate --addon=finnito.module.events and the event_description will be added to the table.

Drop the translatable options if not needed.

Hope that works !!!!

Dave

finnlesueurgmailcom - 2 months ago

Hey @kiltedup!

Thanks for your reply! I get now that you have to add the assignment to the stream definition.

I did as you suggested and added those lines to the migrations and ran php artisan migrate --addon=finnito.module.events but got Nothing to migrate in return.

I imagine this is because the migration name was the same as had previously been run, so I altered the migration file name by a second and ran it again and both the stream and fields migration seemed to work smoothly.

I can see that the field got created successfully in pyrotest_streams_fields (woohoo) but the pyrotest_events_events table has still not been altered and I’m getting the same error.

Thanks for your help so far,

Finn

kiltedup - 2 months ago

Hey!

Dig you include the translatable option?

If so, check table pyrotest_events_events_translations and it should be there!

Dave

finnlesueurgmailcom - 2 months ago

Ah, I did not include the translatable option but I did have a brainwave and ran php artisan addon:reinstall finnito.module.events and it worked!

Thank you very much for helping me out Dave!