gonzo/bakery

A Doctrine ORM to Magento 2 Mapper

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 36

Dependents: 0

Suggesters: 0

Security: 0

Type:magento2-module

0.0.1 2018-09-28 12:00 UTC

This package is auto-updated.

Last update: 2019-10-16 23:14:34 UTC


README

A scaffolding tool for Magento 2. This tool will scaffold a new Magento module based on Doctrine entities you provide. This newly created Magento module will contain everything you need to perform basic CRUD operations in the Magento backend.

This code is currently in development, which means that it could break at any time ¯\_(ツ)_/¯

  • See the Docs for a complete list of all available annotations.
  • See the Roadmap for short term and long term development goals.

Installation

  1. Install the Bakery module with composer require gonzo/bakery --dev
  2. Enable the module with php bin/magento module:enable Gonzo_Bakery
  3. Compile the Magento's DI container php bin/magento setup:di:compile
  4. Setup the module php bin/magento bakery:init which creates a Entities directory in the Magento root and will register some global information.

Create Doctrine entities

  1. Create a Doctrine entity-class in the Entities directory and provide is with the same namespace i.e.: namespace Entities;
  2. Add the use-statement for Doctrine annotations: use Doctrine\ORM\Mapping as ORM;
  3. Compose the entity classes to your specification

Add Bakery specific annotations (optional)

Doctrine annotations will only describe what the database tables will look like and to some extent the form field with which to populate them. For example: the Doctrine annotation @ORM\Column(type="boolean") will create a column with the data type TINYINT(1) and will scaffold a Checkbox form component for the relevant form.

However, in some cases a Doctrine annotation alone will not properly describe what the column is going to be used for. The Doctrine annotation @ORM\Column(type="string", lenght=255) for example, will create a column with the data type VARCHAR(255) which could hold anything from a simple title to the filename of an uploaded file to a color's hex value. In such cases a Bakery annotation could be added to further specify the property's purpose.

Refraining from using any Bakery annotations will result in default form elements. So a simple @ORM\Column(type="string", lenght=255) Doctrine annotation for example, will result in a Input form element.

Bakery annotations are added just like Doctrine annotations and either describe what a form element should look like, or describe what the grid column should look like. Obviously an entity's property could be annotated with both a form element annotation and a column element annotation.

Add a use-statement to the entity-class

To enable bakery annotations add the following use-statement to the entity-class: use Gonzo\Bakery\Annotation as Bakery;

Add a Bakery annotation to the entity-class

Simply add the Bakery annotation to a property's DocBlock. As an example we're going to create a Album entity in order to document our music collection. We're only interested in the album's title and cover art so this entity could look like this:

<?php
namespace Entities;

use Doctrine\ORM\Mapping as ORM;
use Gonzo\Bakery\Annotation as Bakery;

/**
 * @ORM\Entity()
 */
class Album
{
    /**
     * @ORM\Column(name="album_id", length=8, type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id()
     */
    protected $albumId;

    /**
     * @ORM\Column(name="title", type="string")
     */
    protected $title;

    /**
     * @ORM\Column(name="cover", type="string", length=255, nullable=true)
     * @Bakery\FileUploader(fileTypes={"jpg", "jpeg", "gif", "png"}, name="cover")
     */
    protected $cover;

}

Note a FileUploader annotation is added to indicate that a FileUploader component should be used to manage the cover art of our Album table. The annotation describes an array of file extensions that are allowed for this component and the annotation is named cover.

Scaffold a new module

When you're happy with your entity configuration simply create a new module with php bin/magento bakery:scaffold

This command will write the new module in <magento_root>/var/tmp/bakery_output for you to inspect.

When all is good, run php bin/magento bakery:scaffold -w to write it into the app/code directory or copy it manually. Note that any time you re-run php bin/magento bakery:scaffold -w any changes you've made to the generated code will be overwritten. It's your responsibility to commit any changes to a VCS. Generally it's a good idea to fine-tune your entities, run the scaffold command a last time and then make any changes.

Among the generated code is an Setup/InstallSchema.php file which will hold all the database migrations.

Command options:

OptionshorthandDescriptionDefault
--vendor-dThe desired vendor namenone
--module-mThe desired module namenone
--entities-eThe path to the entities directory/Entities
--base-dir-bAbsolute path to directory in which the generated module will be placed/var/tmp/bakery-output

Enable the new module

The new module needs to be enabled by running php bin/magento module:enable YourVendor_YourModule

Since this is a module like any other, these obligatory commands also need to be run to notify Magento of the new code and to run the migrations:

- php bin/magento setup:upgrade
- php bin/magento setup:di:compile
- php bin/magento cache:flush  

FAQ

What does this tool do?

It helps you in creating forms and grids for custom tables. It reads the entity configuration you provide and creates a Magento module in app/code which acts a starting point for you to build on. By setting up all the basic boilerplate code for CRUD operations, you can save time and skip writing all this repetitive error-prone code.

What doesn't this tool do?

  • Bakery does not implement Doctrine into Magento 2.
  • Bakery does not implement the Active Record Pattern into Magento 2 in any way.
  • Bakery will not take into account any entity which has a InheritanceType and a parent class
  • Bakery does not create UpdateSchema or UpdateData classes

For whom is this tool written?

This tool is written for Magento 2 developers who:

  • are familiar with Doctrine
  • need to create a custom module to extend Magento 2 in some way, but are frustrated with the amount of work it takes to create grids and forms from scratch.
  • are fed up with spending too much time debugging a form or grid because the ui-component xml is mis-configured in some way and no useful errors are shown.