Categories


Archives


Recent Posts


Categories


Pestle 1.3 and AbstractModel UI Generation

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This entry is part 6 of 6 in the series Pestle. Earlier posts include Pestle 1.1.1 Released, Pestle 1.1.2 Released, Magento 2 Setup Migration Scripts, Pestle 1.2.1 Released, and Sending Text Messages with PHP, pestle, and Nexmo. This is the most recent post in the series.

A new release of pestle, my PHP command line framework with a full compliment of Magento 2 code generation commands, is available for download. New users will find all they need in the GitHub README.md. Existing users can just run selfupdate

pestle.phar selfupdate

and they’ll be good to go. Issues finished and pull requests accepted this release include

  1. Improvements for a project contributor’s first run experience
  2. Groundwork laid for formatting on all generated XML files
  3. A command to add new columns InstallSchema and UpgradeSchema scripts
  4. A command to add a text field to the UI Component form
  5. A command to add a text column to a UI Component grid
  6. Expansion of magento2:generate:crud-model command to create multiple models in InstallSchema classes
  7. Observers are now generated in top level Observer, not Model, folder
  8. Removed the inevitable One Line Descriptions
  9. A beta effort at bash automated tab expansion for command names

Granular Data Fields

The most important features, and the reason for the 1.2.x to 1.3.x version bump, are the more granular commands for adding fields to your modules features.

In previous (and the most recent) versions of pestle, you could generate code for a CRUD/AbstractModel with the following

$ pestle.phar magento2:generate:crud-model 
Which module? (Pulsestorm_HelloGenerate)] Pulsestorm_GenerateExample
What model name? (Thing)] Thing

However, the InstallSchema command would have the following code

#File: app/code/Pulsestorm/GenerateExample/Setup/InstallSchema.php
$table = $installer->getConnection()->newTable(
    $installer->getTable('pulsestorm_generateexample_thing')
)->addColumn(
    'pulsestorm_generateexample_thing_id',
    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
    null,
    [ 'identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true, ],
    'Entity ID'
)->addColumn(
    'title',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    255,
    [ 'nullable' => false, ],
    'Demo Title'
)->addColumn(
    'creation_time',
    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
    null,
    [ 'nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT, ],
    'Creation Time'
)->addColumn(
    'update_time',
    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
    null,
    [ 'nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE, ],
    'Modification Time'
)->addColumn(
    'is_active',
    \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
    null,
    [ 'nullable' => false, 'default' => '1', ],
    'Is Active'
);

This code generates the base columns any AbstractModel based class in Magento 2 needs, plus a title column as an example for programmers. Until pestle 1.3, programmers would need to manually add columns to this file.

Now, with pestle 1.3, you can use the magento2:generate:ui:add-schema-column to add a column to any PHP source file with a newTable method call.

->newTable(
    $installer->getTable('...')
)->

For example, if we needed another date column in the above InstallSchema.php file, we’d run

$ pestle.phar magento2:generate:ui:add-schema-column
PHP file with newTable call? (skip)] app/code/Pulsestorm/GenerateExample/Setup/InstallSchema.php
Database Table? (packagename_modulename_modelnames) ()] pulsestorm_generateexample_thing
Columns Name? (new_column) ()] birth_date
Column Type?
[1] bigint
[2] boolean
[3] date
[4] datetime
[5] decimal
[6] float
[7] integer
[8] smallint
[9] varchar
[10] varbinary
[11] text
[12] blob
[13] mediumtext
[14] mediumblob
[15] longtext
[16] longblob
 ()] 4
Adding addColumn Call to file

Or, if we wanted to do it in non-interactive mode

$ pestle.phar magento2:generate:ui:add-schema-column app/code/Pulsestorm/GenerateExample/Setup/InstallSchema.php pulsestorm_generateexample_thing birth_date datetime 

Either way, Magento will add the column to the newTable method chain

#File: app/code/Pulsestorm/GenerateExample/Setup/InstallSchema.php

$table = $installer->getConnection()->newTable(
    $installer->getTable('pulsestorm_generateexample_thing')
)
//...
->addColumn('birth_date',
    \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
    null,
    [],
    "birth_date field"
);

This command involves parsing the actual PHP code used in whatever file you’re adding to. If the parser can’t find the code you’re looking for (user error or parser error), pestle will output the addColumn code so you may manually add it to your file

$ pestle.phar magento2:generate:ui:add-schema-column app/code/Pulsestorm/GenerateExample/Setup/InstallSchema.php wrong_table_name birth_date datetime

We couldn't find a newTable call with wrong_table_name
Exiting with an error, but here's the code.

->addColumn('birth_date',
            \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
            null,
            [],
            "birth_date field"
        )

Grids and Forms

Previous versions of Pestle had commands for generating an adminhtml UI grid and UI form for your models, as well as the magento2:generate:full-module command, which outputs every pestle command you’d need for a “full module”.

However, before pestle 1.3, grids defaulted to a single ID column, and forms to a single text column. With pestle 1.3, you can add new text columns to your grid with the magento2:generate:ui:add-column-text

pestle.phar magento2:generate:ui:add-column-text app/code/Pulsestorm/GenerateExample/view/adminhtml/ui_component/pulsestorm_generateexample_things.xml title "Title"

and add new text fields to your form with magento2:generate:ui:add-form-field

pestle.phar magento2:generate:ui:add-form-field
Path to Form XML File? ()] app/code/Pulsestorm/GenerateExample/view/adminhtml/ui_component/pulsestorm_generateexample_things_form.xml
Field Name? (title)] birth_date
Label? (Title)] Birth Date
Fieldset Name? (general)] general

Like all pestle commands, you can run each of these in interactive or non-interactive/argument mode.

With these new additions, it’s now possible to create your Magento 2 module interactively without writing a single line of code yourself. Next steps for pestle are to start adding non-text column-and-field types to the generation routines — if you have opinions about that please let us know via the GitHub Issues. Pestle is still young enough that we’ll be able to take your ideas and turn them into working code by the next release.

Series Navigation<< Sending Text Messages with PHP, pestle, and Nexmo